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 the Author

author image
mkyong
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

avatar
11 Comment threads
5 Thread replies
1 Followers
 
Most reacted comment
Hottest comment thread
13 Comment authors
Greg RsabujJacksonboyshawnjornui Recent comment authors
newest oldest most voted
boyshawn
Guest
boyshawn

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
Guest
Jackson

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

sabuj
Guest
sabuj

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?

jornui
Guest
jornui

Thanks! Very helpful!

George
Guest
George

Thank man. Love your blog.

Sham
Guest
Sham

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

George
Guest
George

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

Greg R
Guest
Greg R

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… Read more »

Greg R
Guest
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… Read more »

Darshan
Guest
Darshan

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

ItsDipak C
Guest
ItsDipak C

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
Guest
Arturo Volpe

Thanks you Mkyon! you rock!!

tkoomzaaskz
Guest
tkoomzaaskz

Your blog rocks! thank you!

Jason Parraga
Guest
Jason Parraga

Thanks for this!

anand
Guest
anand

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

trackback
Weblogic 10.3 cannot find external properties file | PHP Developer Resource