Question

I use the JNI interface to invoke Java code from C code. While compiling I use the following command:

gcc -g -I/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/include/ -I/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/include/linux/ -L/usr/bin/java -L/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/lib/amd64/server/ -ljvm calljava.c

And I use the following code to create the JVM:

JNIEnv* create_vm() 
{
    JavaVM* jvm;
    JNIEnv* env;
    JavaVMInitArgs args;
    JavaVMOption options[1];
    args.version = JNI_VERSION_1_2;
    args.nOptions = 1;
    options[0].optionString = "-Djava.class.path=<classpath>";
    args.options = options;
    args.ignoreUnrecognized = JNI_FALSE;
    JNI_CreateJavaVM(&jvm, (void **)&env, &args);
    return env;
}

My question is: Is the path to the JVM hardcoded in the binary? Can we specify the path to the java executable at runtime? If there is a way to do that can anyone help me with the compile time flags that can be used for that?

Thanks in advance!

Was it helpful?

Solution

The "java executable" is not used at all. When you compile and link your code, you link against a shared library, the location of which is determined by the system at runtime when you launch your executable.

Unless you dynamically load the jvm shared library yourself from a known location (and subsequently look up and call the functions therein), the system is going to determine the "path to the JVM".

Usually if you want to run against a specific version, you would include that version in your application's distribution, and configure the launch of your application to ensure that the proper shared library is used (either via scripts which set the environment appropriately, dynamically loading it, or other system-specific methods).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top