How can I get current value of CLASSPATH
in Groovy?
Asked
Active
Viewed 2.2k times
6 Answers
17
Shameless stolen from http://blog.blindgaenger.net/print_groovys_classpath_for_debugging.html This code will go up the classloader tree and printout each classloader and the associated classpath.
def printClassPath(classLoader) {
println "$classLoader"
classLoader.getURLs().each {url->
println "- ${url.toString()}"
}
if (classLoader.parent) {
printClassPath(classLoader.parent)
}
}
printClassPath this.class.classLoader

M Smith
- 1,988
- 15
- 28
3
You should be able to get the classpath from the SystemClassLoader, providing it is an URLClassLoader:
URL[] classPathUrls = ClassLoader.getSystemClassLoader().getURLs();

Edvin Syse
- 7,267
- 18
- 24
-
1Same thing: both the `CLASSPATH` environment variable and the `-classpath` in the command line have no effect on this list. At least in my case it is identical to what I see in the property `java.class.path`. – Sergey Orshanskiy Oct 11 '13 at 20:21
-
1However, see http://groovy.codehaus.org/How+can+I+dynamically+add+a+library+to+the+classpath about dynamically adding a path to classpath. this.class.classLoader.rootLoader.addURL( new URL("file:///d:/drivers/ojdbc14.jar") ) – Sergey Orshanskiy Oct 11 '13 at 20:39
1
java.class.path
doesn't work properly, at least in Groovy 2.1.6 (Mac OS X 10.6.8).
HelloWorld.groovy
:
public class HelloWorld {
public static void main(def args) {
System.out.println( "Hello, world!\n");
System.out.println(System.getenv("CLASSPATH")+"\n");
System.out.println(System.getProperty("java.class.path"));
}
}
Then
export CLASSPATH=/etc
groovy -classpath /usr HelloWorld.groovy
Result:
Hello, World!
/etc
/Applications/groovy-2.1.6/lib/groovy-2.1.6.jar
Now, this is HelloWorld.java
: (I had to change it a bit as Groovy and Java are not 100% compatible):
public class HelloWorld {
public static void main(String args[]) {
System.out.println( "Hello, world!\n");
System.out.println(System.getenv("CLASSPATH")+"\n");
System.out.println(System.getProperty("java.class.path"));
}
}
Now:
javac HelloWorld.java
export CLASSPATH=/etc
java -classpath /usr HelloWorld
Result:
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld
etc. ...................
Then:
java -classpath /usr:. HelloWorld
Result:
Hello, world!
/etc
/usr:.
I'll update if I find out how to make it work in Groovy...

Sergey Orshanskiy
- 6,794
- 1
- 46
- 50
0
This doesn't work?
System.getProperty('java.class.path')

Tomasz Nurkiewicz
- 334,321
- 69
- 703
- 674
-
3
-
3Groovy seems to do funky stuff, and the java.class.path does not contain the classpath passed with groovy's -cp command line option. – Egon Willighagen Sep 20 '11 at 12:41
-
2It shows only groovy jar - why http://stackoverflow.com/a/27131375/1042297 is not accepted answer ? – kodstark Aug 12 '16 at 17:13
0
Get the CLASSPATH and files if you want in the those CLASSPATH if needed you can view it
System.getProperty("java.class.path", ".").tokenize(File.pathSeparator).each {
println it
}

anish
- 6,884
- 13
- 74
- 140
-1
def classpath = System.properties["java.class.path"]

Bozho
- 588,226
- 146
- 1,060
- 1,140
-
Unfortunately it's empty when I'm using Groovy inside [gmaven plugin](http://docs.codehaus.org/display/GMAVEN/Executing+Groovy+Code). Is it possible to get classpath from class loader somehow? – yegor256 Mar 06 '11 at 18:55