简体   繁体   中英

How to get a list of jar files and folders in classpath in Java 9?

I know you can't change the classpath in Java 9 because I read this: Add jar to classpath at runtime under java 9

I just want to list jar files and folders currently on the Classpath so that I can build a command line argument to the Java compiler.

// Construct compile command options
// According to: http://java.sun.com/j2se/1.5.0/docs/tooldocs/solaris/javac.html
// The directory specified by -d is not automatically added to your
// classpath, so we'll add it manually.
String[] args = new String[] {"-d", classDir,
                              "-classpath", classPath,
                              "-encoding", "UTF-8",
                              srcFile};

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
int ret = compiler.run(null, out, err, args);

To construct a classpath in Java 8 I casted to URLClassLoader which is illegal in Java 9:

ClassLoader myCl = SimpleJavaCompilerTest.class.getClassLoader();
URLClassLoader myUcl = (URLClassLoader) myCl;
for (URL url : myUcl.getURLs()) {
    System.out.println(url.toString().replace("file:", ""));
}

This produced (GOOD) output like:

/tools/idea-IU-181.4445.78/lib/idea_rt.jar
...
/tools/idea-IU-181.4445.78/plugins/junit/lib/junit5-rt.jar
/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/charsets.jar
...
/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/rt.jar
/blah/myApp/target/test-classes/
/blah/myApp/target/classes/
/home/me/.m2/repository/commons-fileupload/commons-fileupload/1.3.3/commons-fileupload-1.3.3.jar
...
/home/me/.m2/repository/org/slf4j/slf4j-api/1.7.25/slf4j-api-1.7.25.jar

I tried: Scanning classpath/modulepath in runtime in Java 9 but it produced a list of ~19,000 class files that are part of openjdk, but not the jar files or class folders from my project. Like (BAD):

java/awt/desktop/ScreenSleepListener.class
...
javax/accessibility/AccessibilityProvider.class
...
jdk/dynalink/beans/AbstractJavaLinker$1.class
...
META-INF/providers/org.graalvm.compiler.code.HexCodeFileDisassemblerProvider
...
module-info.class
...
netscape/javascript/JSException.class
...
org/graalvm/compiler/api/directives/GraalDirectives.class
...
org/ietf/jgss/ChannelBinding.class
...
org/jcp/xml/dsig/internal/SignerOutputStream.class
...
org/w3c/dom/xpath/XPathResult.class
...
org/xml/sax/AttributeList.class
...
sun/applet/AppContextCreator.class
...
sun/util/spi/CalendarProvider.class
...

I need the jar files and the class folders specific to my project (which are not in the java-9 list above). How do I get that in Java 9?

this below code snippet would help to get the desired output.

String classpath = System.getProperty("java.class.path");
String[] classPathValues = classpath.split(File.pathSeparator);
    for (String classPath: classPathValues) {
      System.out.println(classPath);
    }

In Java 9+ you can get classpath URLs with a little help from reflection:

ClassLoader classLoader = ClassLoader.getSystemClassLoader();
Field ucpField = BuiltinClassLoader.class.getDeclaredField("ucp");
ucpField.setAccessible(true);
URLClassPath classpath = (URLClassPath) ucpField.get(classLoader);
for (URL url : classpath.getURLs()) {
    System.out.println(url.getFile());
}

Don't forget to add:

  • requires java.base to your module-info.java file
  • --add-exports java.base/jdk.internal.loader=your_module_name compiler option
  • --add-exports java.base/jdk.internal.loader=your_module_name and --add-opens java.base/jdk.internal.loader=your_module_name VM options

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM