Java – jar in classpath
•
Java
Is there a way to programmatically get a complete list of all jars in the classpath?
I need this because we have behavioral differences between the two platforms One is managed by us, but the other is not. I need to know if it exists or not the same jar
Solution
This is a way to print out the current project classpath:
ClassLoader cl = ClassLoader.getSystemClassLoader(); java.net.URL[] urls = ((java.net.urlclassloader)cl).getURLs(); for (java.net.URL url: urls) { System.out.println(url.getFile()); }
If you need to determine which jar a class is loaded from:
Class klass = ClassInTrouble.class; CodeSource source = klass.getProtectionDomain().getCodeSource(); System.out.println(source == null ? klass : source.getLocation());
You can also get a list of all classes loaded in the JVM if it is useful
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码