Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can JNI be made to honour wildcard expansion in the classpath?

I have a C binary that calls out to Java via JNI. I set CLASSPATH to somedir/* to pick up all the jars in somedir.

When I run the binary, a required class definition cannot be found. When I run

java that.class's.name 

from the same command line, the class is successfully found. If I explicitly add all the jars in somedir/ to the classpath, everything works great, but that leads to a very long classpath which I'd like to avoid.

Does a JVM executed via JNI honour wildcard expansion of the classpath? Can it be made to do so?

like image 917
HenryR Avatar asked Feb 16 '12 23:02

HenryR


2 Answers

I figured out the answer by reading the hotspot source code.

Only paths passed via either CLASSPATH or -cp / -classpath are subject to wildcard expansion. These are then passed as a system property to the running JVM via -Djava.class.path.

You tell a JNI-invoked JVM about a classpath via a JVMOptions structure, which may include -Djava.class.path but -classpath will not necessarily be honoured (and in practice, isn't by the hotspot implementation). Since java.class.path is directly passed to the JVM as a system property, it doesn't get wildcard expanded and therefore wildcards won't work.

like image 109
HenryR Avatar answered Oct 31 '22 19:10

HenryR


No. No, it cannot. Using JNI doesn't help.

The way you would do this is by implementing your own class loader (in Java), but that class loader would have to be in the wildcard-free CLASSPATH.

You could, of course, set the CLASSPATH to its expanded form before invoking the JVM. That would work and could be done via a shell script (no JNI needed).

like image 3
Borealid Avatar answered Oct 31 '22 20:10

Borealid