Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting CLASSPATH during runtime

Tags:

java

.net

ikvm

How do I set a CLASSPATH variable during runtime while using IKVM?

I've been trying to do it by using: java.lang.System.setProperty("java.class.path", "whatever");

The class I'm calling requires a configuration file in the classpath to work - and I keep getting errors that seem to indicate that it hasn't gotten its settings.

Is the way I'm trying to add variable incorrect?

like image 420
Ciddan Avatar asked May 21 '26 20:05

Ciddan


1 Answers

If you really can't set the classpath beforehand yourself using the java's -cp or -classpath argument (why not by the way? that's the normal approach), then you can try to use URLClassLoader instead. Here's a kickoff example:

URL url = new URL(whateverPath);
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
ClassLoader urlCL = URLClassLoader.newInstance(new URL[] { url }, contextCL);
Thread.currentThread().setContextClassLoader(urlCL);
// ...

You only need to be lucky if the class you're calling is actually loading its resources through Thread.currentThread().getContextClassLoader().getResource() and thus not through SomeClass.class.getClassLoader().getResource().

like image 188
BalusC Avatar answered May 24 '26 10:05

BalusC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!