Vyacheslav
Vyacheslav

Reputation: 1256

Running JVM from C++ code and setting classpath

I'm trying to create JVM 7 from C++ code and struggling with setting the right classpath. I want to specify a classpath using wildcards: e.g. /path/to/* (to include all the jars in the folder to the classpath)

If I'm setting a classpath via

options[0].optionString = "-Djava.class.path=/path/to/*;"; 

then my class is not found. I tried backslashes (I'm using Windows 8), with and without semicolon, nothing helped. This actually does not work from a command line either.

Then I tried to provide a "-cp" option, but in this case a JVM failed to be created. I tried:

options[0].optionString = "-cp=/path/to/*";

options[0].optionString = "-cp /path/to/*"; 

options[0].optionString = "-classpath=/path/to/*"; 

options[0].optionString = "-classpath /path/to/*"; 

options[0].optionString = "-cp"; 
options[0].extraInfo = "/path/to/*"; 

options[0].optionString = "-cp"; 
options[1].optionString = "/path/to/*"; 

None of those helped.

Do you have an idea how to provide a classpath with wildcards when creating a JVM from C++?

Thanks in advance

Upvotes: 6

Views: 2928

Answers (3)

cjg
cjg

Reputation: 116

If we let the shell expand the wildcard and there is more than a jar file it will not work, in fact for example if in /path/to/jars there are A.jar and B.jar and say we want to use C class contained in one of the jar files and we try to run (linux):

java -cp /path/to/jars/* C

it will be expanded as

java -cp /path/to/jars/A.jar /path/to/jars/B.jar C

java will complain that it cannot find the class /path/to/jars/B.jar.

But if (again on linux, so on windows you should replace ":" with ";"), following the documentation pointed by deviantfan, I execute the command

java -cp /path/to/jars/*.jar: C

it works properly (in this case is not the shell but the jvm initialization that expands the wildcard). So I suppose on windows it should work if you append a ";" to the "*" (as in "-cp /path/to/jar/*;").

Upvotes: 0

Brett Kail
Brett Kail

Reputation: 33936

You will need to perform the expansion yourself because this is a feature of the Java launcher, not a feature of the JNI API. See the SetClassPath function in the launcher source, which calls the internal JLI_WildcardExpandClasspath function and then adds a -Djava.class.path option.

Upvotes: 7

cjg
cjg

Reputation: 116

The problem is not related to C++, in fact if you try a similar command (as in java -cp /path/to/jars/* my.awesome.project.Main) from the command line (windows or any other OS) you'll have the same issue.

Sadly the jvm initialization doesn't support wildcards, so you'll have to scan the directory by yourself and build the string containing the paths of the jar files.

Upvotes: -1

Related Questions