Strange behavior of Class.getResource() and ClassLoader.getResource() in executable jar

I thought this question was already asked and answered! What is the difference between Class.getResource() and ClassLoader.getResource()? getClass().getResource() searches relative to the .class file while getClass().getClassLoader().getResource() searches relative to the classpath root. If there’s an SSCCE here, I don’t understand why it doesn’t 1) Show the directory organization in the .jar, and… 2) Take package … Read more

How do I create a parent-last / child-first ClassLoader in Java, or How to override an old Xerces version that was already loaded in the parent CL?

Today is your lucky day, as I had to solve this exact problem. I warn you though, the innards of class loading are a scary place. Doing this makes me think that the designers of Java never imagined that you might want to have a parent-last classloader. To use just supply a list of URLs … Read more

Order of loading jar files from lib directory

It’s all described in Tomcat’s ClassLoading HOW-TO. It’s not necessarily in alphabetic order. If you observed that behaviour, it should absolutely not be relied upon if you intend to keep your webapp portable across servers. For example, Tomcat 6 “coincidentally” orders it, but Tomcat 8 does not. Summarized, the loading order is as follows: bootstrap/system … Read more

When and how is a java classloader marked for garbage collection?

I always heard that Classloader unloading was problematic. They are theoretically garbage collected when there is not reference to the object instances and class unloading is not necessary, but in practice it seems like to be more problematic. Subtle references may leak and prevent the Classloader from being reclaimed. In application servers, after numerous redeploy … Read more

Java: Difference between Class.forName and ClassLoader.loadClass

Class.forName() will always use the ClassLoader of the caller, whereas ClassLoader.loadClass() can specify a different ClassLoader. I believe that Class.forName initializes the loaded class as well, whereas the ClassLoader.loadClass() approach doesn’t do that right away (it’s not initialized until it’s used for the first time). Just found this article when looking to confirm my summary … Read more

How can I list all classes loaded in a specific class loader

Try this. It’s a hackerish solution but it will do. The field classes in any classloader (under Sun’s impl since 1.0) holds hard reference to the classes defined by the loader so they won’t be GC’d. You can take a benefit from via reflection. Field f = ClassLoader.class.getDeclaredField(“classes”); f.setAccessible(true); ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); Vector<Class> classes … Read more

In Java, is it possible to know whether a class has already been loaded?

(Thanks to Aleksi) This code: public class TestLoaded { public static void main(String[] args) throws Exception { java.lang.reflect.Method m = ClassLoader.class.getDeclaredMethod(“findLoadedClass”, new Class[] { String.class }); m.setAccessible(true); ClassLoader cl = ClassLoader.getSystemClassLoader(); Object test1 = m.invoke(cl, “TestLoaded$ClassToTest”); System.out.println(test1 != null); ClassToTest.reportLoaded(); Object test2 = m.invoke(cl, “TestLoaded$ClassToTest”); System.out.println(test2 != null); } static class ClassToTest { static { … Read more