Java – Unable to create JVM using jni_CreateJavaVM API in Fedora 8

fedorajava-native-interfacephp-extension

I have created a PHP extension (written in C). I am using JNI to make java calls.

I use jni_CreateJavaVM to create JVM. This works fine in:

  • IIS (windows)
  • Apache (windows)
  • PHP CLI (Fedora 8)

But when I try to load the same extension from Apache (in Fedore 8), the create jni_ CreateJavaVM return JNI _ERROR. I call jni _CreateJavaVM from PHP _RINIT _FUNCTION.

I though that it might be that my extension cannot loacate libjvm.so. So in the same code, before calling jni_ CreateJavaVM, I tried to load the libjvm.so library, and it was successful. Only when I call the jni function, it returns JNI_ERROR.

I used LD_ LOAD function to load libjvm.so. After loading the library I obtain pointer to jni_ CreateJavaVM method which is successful. Only when I make a call, using the pointer or direct jni_ CreateJavaVM call, it returns -1.

Is there any way to find out what went wrong during initialization?

Platfrom: Fedora 8
Java: Jdk 1.5 update 8
Php: Php version 5.3

Here is the code that I have written to initialize JVM.

typedef jint (JNICALL CreateJavaVM_t)(JavaVM **pvm, void **env, void *args);
typedef jint (JNICALL* GetDefaultJavaVMInitArgs_t)(void*);

CreateJavaVM_t *CreateJavaVM;
GetDefaultJavaVMInitArgs_t GetDefaultJavaVMInitArgs;
JavaVM* jvm;
JNIEnv* env;

void create_vm(void)
{       
    JavaVMOption vm_options;
    JavaVMInitArgs vm_args;
    int retval = 0;

    dl_handle = DL_LOAD("libjvm.so");       
    //The call is successful and I get the handle

    CreateJavaVM = (CreateJavaVM_t*)DL_FETCH_SYMBOL(dl_handle, "JNI_CreateJavaVM");
    //The call is successful and I get pointer to function

    GetDefaultJavaVMInitArgs = (GetDefaultJavaVMInitArgs_t)DL_FETCH_SYMBOL(dl_handle,"JNI_GetDefaultJavaVMInitArgs");
    //The call is successful and I get pointer to function

    vm_args.version = JNI_VERSION_1_4;
    vm_args.options = &vm_options;
    vm_args.nOptions = 0;
    vm_args.ignoreUnrecognized = JNI_FALSE;

(*GetDefaultJavaVMInitArgs)(&vm_args);

    retval = CreateJavaVM(&jvm, (void **)&env, &vm_args);
    //The retval is -1.

    //And if I do
    retval = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
    //This also returns -1
}

Best Answer

This question is five years old. I stumbled across it while researching about the status of the function JNI_GetDefaultJavaVMInitArgs on different platforms. It seems to be a function that is unnecessary to call, and may even be deprecated on some platforms. But using the current documentation from Oracle (for Java 1.7 and 1.8) as the primary reference, it's clear that the above code has a serious bug. Only the vm_args.version field should be set before the call to JNI_GetDefaultJavaVMInitArgs. All of the other fields should be set after calling JNI_GetDefaultJavaVMInitArgs.

But on most platforms you can probably simply remove the call to JNI_GetDefaultJavaVMInitArgs.

Related Topic