3

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?

apaderno
  • 28,547
  • 16
  • 75
  • 90
Ciddan
  • 374
  • 5
  • 19

2 Answers2

3

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().

yegor256
  • 102,010
  • 123
  • 446
  • 597
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • The reason I can't set the classpath beforehand is that I'm running the java code in .Net (jar-files recompiled to .net assemblies) with the IKVM VirtualMachine. IKVM does not read from the classpath. The example above would work great for any Java-code though :) – Ciddan May 25 '10 at 09:40
0

I was trying to do the same thing. I had some jar files compiled to a .Net dll but some of those (3rd party) jar files were trying to load their configuration files from the java classpath.

I solved the problem by specifying the -classloader option for the ikvmc tool. Example:

ikvmc -out:mydotnetapp.dll -classloader:ikvm.runtime.ClassPathAssemblyClassLoader c:/myjavaapp/lib/*.jar

This worked for me!

Source for the solution: http://old.nabble.com/Not-able-to-load-files-from-ClassPath-td31141788.html

tsaixingwei
  • 636
  • 9
  • 13