javajvmvirtual-machineclassloaderclassloading

How do I obtain classpaths from a VirtualMachine?


I have a VirtualMachine from which I want to extract "classpaths". The VM in this case is launched using a RawCommandLineLauncher.

The closest thing I could find is the VirtualMachine method allClasses, but I would like one step above that and get a list of class directories instead. Is this possible?

I tried:


Solution

  • You can check and cast to a subinterface, PathSearchingVirtualMachine

    if (vm instanceof PathSearchingVirtualMachine psvm) {
        List<String> cp = psvm.classPath();
    }
    

    Above example uses instanceof pattern matching, which is a newer language feature. If you're on an older version of Java then do a normal cast.