How to check that .jar class or class is already loaded by ClassLoader in Java

.. I have an application (.war) in which there is a plug-in system where each plug-in is a .jar with its .jar dependencies. And in webapp, through the admin web interface, plugins are loaded using ClassLoader, which “loads” .jar and its dependencies. But the problem is that, for example, 2 plugins A and B can have one or more common dependencies (commons-io.jar, commons-collection.jar, ...) ... so usually I should not download more, than once .jar that were already loaded by another plugin or which were already present in the web store itself → WEB-INF / lib / So, I would like to know if there is a way not to load another time .jar that has already been loaded via webapp or another plugin If possible, how should I do it!

Actually, no matter what solution is available, I just want to avoid problems with the bootloader.

NOTE: each plugin is a .zip file that has the following architecture

plugin name /

- code/ - A.class - B.class - ... - lib/ - commons-io.jar - log4j.jar - .... - description.xml - version.txt 

every .class file in the / directory is loaded by the class loader, every .jar in the lib / directory of the plugin is also loaded.

But, as a rule, I should not reload the classes present in the .jar "commons-io.jar" if it has ever been present ...

I hope I was clear enough in advance for your attention and help.

+4
source share
3 answers

To keep track of which classes are loaded by the JVM, you can add

 -verbose:class Display information about each class loaded. 

To your parameters before starting the JVM.

This wille creates output, for example

 [Opened C:\Program Files\Java\jre6\lib\rt.jar] [Loaded java.lang.Object from C:\Program Files\Java\jre6\lib\rt.jar] [Loaded java.io.Serializable from C:\Program Files\Java\jre6\lib\rt.jar] [Loaded java.lang.Comparable from C:\Program Files\Java\jre6\lib\rt.jar] [Loaded java.lang.CharSequence from C:\Program Files\Java\jre6\lib\rt.jar] [Loaded java.lang.String from C:\Program Files\Java\jre6\lib\rt.jar] 
+6
source

The Sun Microsystems compatible JVM will also show classes at boot time if you use the -verbose:class JVM option. However, you cannot access this information programmatically.

+1
source

If I understand correctly that you want to know if a Java or jar file is loaded or not, if it is not loaded, then you want to download it? One of the solutions I get seems to be that you can check if the jar is present in the classpath or not.

0
source

Source: https://habr.com/ru/post/1381253/


All Articles