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

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

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)