0

Java 11.0.8 2020-07-14

Code

public class MainApplication {
    public static void main(String[] args) {
        //Get the System Classloader
        ClassLoader sysClassLoader = ClassLoader.getSystemClassLoader();

        //Get the URLs
        URL[] urls = ((URLClassLoader)sysClassLoader).getURLs();

        for(int i=0; i< urls.length; i++)
        {
            System.out.println(urls[i].getFile());
        }      }
}

Stack trace

Connected to the target VM, address: '127.0.0.1:49215', transport: 'socket'
Exception in thread "main" java.lang.ClassCastException: class jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to class java.net.URLClassLoader (jdk.internal.loader.ClassLoaders$AppClassLoader and java.net.URLClassLoader are in module java.base of loader 'bootstrap')
    at com.example.MainApplication.main(MainApplication.java:30)
Disconnected from the target VM, address: '127.0.0.1:49215', transport: 'socket'

I just want to print the classpath. How can I do that?

halfer
  • 19,824
  • 17
  • 99
  • 186
Trts
  • 985
  • 1
  • 11
  • 24

0 Answers0