Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get classpath from classloader?

I am using some third party code which when given a '-classpath' command line argument doesnt set the java.class.path, but instead just creates a classloader, adds all the urls for the items on the command line specified classpath to the classloader, and then sets it to be the context classloader. In a plugin class to this code that I have written, I get an instance of this classloader, and somehow need to use it to get back the underlying classpath, so that I can use it in an invocation of JavaCompiler.getTask(...) and compile some other code on the fly. However there doesn't seem to be anyway to get the ClassPath from the ClassLoader, and as java.class.path is unset, I can't seem to access the underlying classpath that the application was initially invoked with...Any ideas?

like image 791
Marcus Mathioudakis Avatar asked Jul 23 '12 13:07

Marcus Mathioudakis


People also ask

How do I find the classpath?

In short, to get the classpath using System class you should: Use the getProperty(String key) , for the java. class. path key.

How do I list all classes in a classpath?

How can I get list of all available classes in CLASSPATH at runtime? In Eclipse IDE, you can do this by pressing Ctrl + Shift + T .


1 Answers

If the classloader uses URLs, it must be a URLClassloader. What you have access to is the URLs which defines the classpath for it along side with its parent ClassLoader.

To get the URLs, simply do the following:

((URLClassLoader) (Thread.currentThread().getContextClassLoader())).getURLs() 
like image 148
Adel Boutros Avatar answered Oct 11 '22 06:10

Adel Boutros