Main Tutorials

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
...

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
17 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
boyshawn
4 years ago

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’)

Jackson
4 years ago
Reply to  boyshawn

As per “Sham” comment, this works;
System.out.println(System.getProperty(“java.class.path”).replace(‘:’,’\n’));

sabuj
4 years ago
Reply to  Jackson

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?

pen zyc
3 years ago

not working in java 11?

Riley
2 years ago

Thanks again, MKyong, very useful !

jornui
5 years ago

Thanks! Very helpful!

George
6 years ago

Thank man. Love your blog.

Sham
6 years ago

System.getProperty(“java.class.path”), the class path printed by this, Will it be correct?

Greg R
4 years ago
Reply to  Sham

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…)

Greg R
4 years ago
Reply to  Greg R

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.

George
6 years ago
Reply to  Sham

Yes, it works but you need to tokenize the output before printing it out using the colon (:) as the separator.

Darshan
8 years ago

There’s a typo. It says “pint out” instead of print out.

ItsDipak C
9 years ago

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..

Arturo Volpe
10 years ago

Thanks you Mkyon! you rock!!

tkoomzaaskz
11 years ago

Your blog rocks! thank you!

Jason Parraga
11 years ago

Thanks for this!

anand
11 years ago

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