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, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
10 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
9 Comment authors
jornuiGeorgeShamDarshanItsDipak C Recent comment authors
newest oldest most voted
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.

jornui
Guest
jornui

Thanks! Very helpful!

George
Guest
George

Thank man. Love your blog.

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