Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get real classpath when running java application with mvn exec:java

My java application needs to access the classpath via System.getProperty("java.class.path"). This works perfect when I run the application from IDE. However if I run the application with maven via mvn exec:java, I found the classpath returned is always /usr/share/maven/boot/plexus-classworlds-2.x.jar.

Question is how can I get my real classpath when the app started by mvn exec:java?

Updates: I end up calling URLClassLoader.getURLs() to get the classpath

like image 262
Gelin Luo Avatar asked Mar 11 '26 20:03

Gelin Luo


1 Answers

the hint in your update worked for me. here's my implementation for my fork of kilim.tools.Javac, ie a wrapper around ToolProvider.getSystemJavaCompiler()

static String getClassPath() {
    String cp = "";
    ClassLoader sys = ClassLoader.getSystemClassLoader();
    ClassLoader cl = Javac.class.getClassLoader();
    for (; cl != null & cl != sys; cl = cl.getParent())
        if (cl instanceof java.net.URLClassLoader) {
            java.net.URLClassLoader ucl = (java.net.URLClassLoader) cl;
            for (java.net.URL url : ucl.getURLs())
                cp += File.pathSeparator + url.getPath();
        }
    return cp.length()==0 ? null : cp.substring(1);
}

the result get's passed to the compiler as compiler.run(null, null, null, arg1, arg2, "-cp", getClassPath())

like image 71
nqzero Avatar answered Mar 13 '26 10:03

nqzero



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!