Get real classpath when starting java application using mvn exec: java

My Java application needs to access the classpath via System.getProperty("java.class.path")

. This works great when I run the application from the IDE. However, if I run the application with maven via mvn exec:java

, I find that the returned classpath is always /usr/share/maven/boot/plexus-classworlds-2.x.jar

.

Question: how can I get my real classpath when the application is started with mvn exec:java

?

Updates: I end up calling URLClassLoader.getURLs()

to get the classpath

+3


source to share


1 answer


the hint in your update worked for me. here's my implementation for my fork kilim.tools.Javac

, i.e. a wrapper aroundToolProvider.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 is passed to the compiler as compiler.run(null, null, null, arg1, arg2, "-cp", getClassPath())

+1


source







All Articles