How to get SystemClassLoader URLs in JDK17 gracefully?

158 Views Asked by At

Now i am switch Jdk8 to Jdk17. In Jdk8 can use this follow code to get SystemClassLoader Urls.

private static URL[] getURLClassPath() {
    ClassLoader classLoader = ClassLoader.getSystemClassLoader();
    return ((URLClassLoader) classLoader).getURLs();
}

but in Jdk17 , the default SystemClassLoader(AppClassLoader) is not a subclass of URLClassLoader, now i use this code to get the Urls refer to jdk17.

private static URL[] getURLClassPath() {
    String property = System.getProperty("java.class.path");
    URL[] urls = Arrays.stream(property.split(";")).map(u -> {
        try {
            return Path.of(u).toRealPath().toFile().toURI().toURL();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }).toArray(URL[]::new);
    return urls;
}

but i always felt it was inelegant. so have any elegant method ? best wishes and thanks a lot.

Need find any elegant method to get SystemClassLoader URLs in Jdk17.

0

There are 0 best solutions below