How to safely access the URLs of all resource files in the classpath in Java 9/10?

前端 未结 2 582
无人及你
无人及你 2020-12-24 11:21

We learned from the release notes of Java 9 that

The application class loader is no longer an instance of java.net.URLClassLoader (an implementation

2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-24 11:35

    AFAIK you can parse the java.class.path system property to get the urls:

    String classpath = System.getProperty("java.class.path");
    String[] entries = classpath.split(File.pathSeparator);
    URL[] result = new URL[entries.length];
    for(int i = 0; i < entries.length; i++) {
        result[i] = Paths.get(entries[i]).toAbsolutePath().toUri().toURL();
    }
    
    System.out.println(Arrays.toString(result)); // e.g. [file:/J:/WS/Oxygen-Stable/jdk10/bin/]
    

提交回复
热议问题