How to print out the current project classpath
Java’s “SystemClassLoader” can use to pint out the current project classpath , indirectly display the library dependency as well.
Example
package com.mkyong.io;
import java.net.URL;
import java.net.URLClassLoader;
public class App{
public static void main (String args[]) {
ClassLoader cl = ClassLoader.getSystemClassLoader();
URL[] urls = ((URLClassLoader)cl).getURLs();
for(URL url: urls){
System.out.println(url.getFile());
}
}
}
Output
/E:/workspace/HibernateExample/target/test-classes/
/E:/workspace/HibernateExample/target/classes/
/D:/maven/repo/antlr/antlr/2.7.7/antlr-2.7.7.jar
/D:/maven/repo/asm/asm/3.1/asm-3.1.jar
/D:/maven/repo/cglib/cglib/2.2/cglib-2.2.jar
...
This doesn’t work in Java 12 anymore. Got an exception:
Exception in thread “main” java.lang.ClassCastException: class jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to class java.net.URLClassLoader (jdk.internal.loader.ClassLoaders$AppClassLoader and java.net.URLClassLoader are in module java.base of loader ‘bootstrap’)
As per “Sham” comment, this works;
System.out.println(System.getProperty(“java.class.path”).replace(‘:’,’\n’));
copy pasteable into jshell, the one above has some unicode chars which it doesn’t like :
System.out.println(System.getProperty(“java.class.path”).replace(‘:’, ‘\n’));
well that’s annoying, is it my browser charset or some default on the website commenting system?
not working in java 11?
Thanks again, MKyong, very useful !
Thanks! Very helpful!
Thank man. Love your blog.
System.getProperty(“java.class.path”), the class path printed by this, Will it be correct?
It’s “correct” in some sense, but I know of at least two cases where it’s not what you really want:
(1) On systems where the classpath length may exceed command-line or other limits, the system property may include at least one level of indirection via the Class-Path keyword in a jar’s manifest. For example, Samza 1.1 and later stuff the entire classpath into META-INF/MANIFEST.MF in pathing.jar, and therefore java.class.path consists only of a conf dir and “pathing.jar”. I’m not sure whether jars specified in a manifest can themselves specify additional jars in their own manifests, but having to recurse through all of that manually would certainly be tedious.
(2) Web apps launched within a Jetty container see the outer container’s classpath, not their own. In fact, I’m about to test whether mkyong’s ClassLoader approach gets around that. (It should…)
After further experimentation:
(1) The system class loader and System.getProperty(“java.class.path”) appear to be identical, at least for the cases I tested.
(2) As a corollary, mkyong’s approach using the _system_ class loader isn’t useful for web apps inside a Jetty server, but with a small tweak it is: this.getClass().getClassLoader() (or Thread.currentThread().getContextClassLoader()) returns the “local” class loader (WebAppClassLoader, which is a subclass of URLClassLoader), and for Jetty-container web apps that one does give you the true classpath (basically the contents of the .war).
(3) There doesn’t seem to be a way to get the system class loader to cough up the effective classpath, i.e., what you would get after aggregating and perhaps recursing over all of the classpaths stored in jar metafiles. So the “pathing jar” case requires manual enumeration and processing of classLoader.getResources(JarFile.MANIFEST_NAME), at least as far as I know.
Yes, it works but you need to tokenize the output before printing it out using the colon (:) as the separator.
There’s a typo. It says “pint out” instead of print out.
Hi MKYong,
I Tried your code, its working fine when running from jar, but when i create executable , its giving giving just exe file path, please help, I want classpath of the project contain in jar. I created exe using jar2exe application. also tried with JLauncher still giving same error.
Waiting for ur reply..
Thanks you Mkyon! you rock!!
Your blog rocks! thank you!
Thanks for this!
wow, thanks a lot my friend.. it worked perfect.. you are awesome..
I have a related question.. you see why should i add a jar in tomcats classpath? i mean, i add it to the buildpath(the applications classpath). i dont understand why should it needs to be added in the classpath of tomcat?
The jars i am talking about are
/C:/Greenvi_trial2/greevi_webapp/WebContent/WEB-INF/lib/commons-io-2.4.jar
/C:/Greenvi_trial2/greevi_webapp/WebContent/WEB-INF/lib/commons-codec-1.5.jar
Cheers,
-Anand