H
Hf
Hi there
I am having a strange problem with JNI on Solaris 8 & Solaris 9 (JDK
1.4.1_02) with the recommended J2SE patch applied
My code works fine if I Create a JVM and then proceed to invoke my Java code
from the env retruned by JNI_CreateJavaVM. However as soon as I deatach the
current thread from the Vm and then re-attach using AttachCurrentThread my
Java throws an exception as shown below
Exception in thread "Thread-0" javax.xml.parsers.FactoryConfigurationError:
Provider org.apache.xerces.jaxp.DocumentBuilderFactoryImpl not found
at
javax.xml.parsers.DocumentBuilderFactory.newInstance(DocumentBuilderFactory.
java:99)
Now nothing to do with classpaths or anything else has changed so why am I
getting an error just because I used AttachCurrentThread ?
If I replace the Java code with a very basic class that does nothing but
output to the console and calls some native functions everything works fine
so maybe it is something to do with the AttachCurrentThread and JAX but I'm
at a loss.
Exactly the same code works perfectly on Windows XP.
This is actually a simplification of the final problem which will be
attaching from multiple threads to the JVM to run the Java. By calling
DetachCurrentThread followed by AttachCurrentThread I am getting the same
error without the complication of the extra threads.
My code is summarized below (i've removed error checks for clarity). Can
anyone suggest a reason why this error occurs on Solaris.
TIA
Adrian
// The Global JVM Pointer
static JavaVM *theJVM = NULL;
JavaVM* nsvr_GetPointerToTheJVM()
{
if(theJVM != NULL)
{
return theJVM;
}
else
{
JNIEnv *env = NULL;
jint res;
JavaVMInitArgs vm_args;
JavaVMOption options[2];
options[0].optionString = "-Djava.class.path=" OCTOPUS_CLASSPATH;
options[1].optionString =
"-DOCTOPUS_HOME=/export/home/buildmc/testPas_Adrian/Octopus";
vm_args.version = 0x00010002;
vm_args.options = options;
vm_args.nOptions = 2;
vm_args.ignoreUnrecognized = JNI_TRUE;
res = JNI_CreateJavaVM(&theJVM, (void**)&env, &vm_args);
if (res < 0)
return NULL; // Can't create Java VM
//Comment the following line out and the Java runs fine
theJVM->DetachCurrentThread();
return theJVM;
}
}
int doOctopus(char* pOctoFilename)
{
JavaVM *jvm = NULL;
JNIEnv *env = NULL;
jint res, res2;
jclass cls;
jmethodID mid, mid2, mid3, clsCons;
jstring jstr, jstr2, jstr3;
jclass stringClass;
jobjectArray args, args2, args3;
jstring jStr4;
const char *str;
jobject clsInstance;
int rc = 0;
const JNINativeMethod methods[] = {
{
"publishProgress",
"(Ljava/lang/StringI",
&publishProgress__String
},
{
"publishProgress",
"(II)I",
&publishProgress__II
},
{
"publishProgress",
"(IIF)I",
&publishProgress__IIF
}
};
printf("In doOctopus()!\n");
jvm = nsvr_GetPointerToTheJVM();
if(jvm != NULL)
jvm->AttachCurrentThread((void **)&env, NULL);
if(jvm != NULL && env != NULL)
{
cls = (*env).FindClass("org/webdocwf/util/loader/Loader");
if (cls != 0)
{
res2 = env->RegisterNatives(cls, methods, 3); //Register Native
methods for progressFeedback
mid = (*env).GetStaticMethodID(cls, "main","([Ljava/lang/StringV");
jstr = (*env).NewStringUTF(pOctoFilename);
stringClass = (*env).FindClass("java/lang/String");
args = (*env).NewObjectArray(1, stringClass, jstr);
(*env).CallStaticVoidMethod( cls, mid, args);
}
if ((*env).ExceptionOccurred())
{
(*env).ExceptionDescribe();
}
if (cls != 0)
res2 = env->UnregisterNatives(cls);
res = jvm->DetachCurrentThread();
}
return 0;
}
int main(int argc, char* argv[])
{
char* octoFile1 =
"/export/home/buildmc/testPas_Adrian/Octopus/sInstoreJob.xml";
printf("C++ Hello from main()!\n");
int iRes = 0;
iRes = doOctopus(octoFile1);
return iRes;
}
I am having a strange problem with JNI on Solaris 8 & Solaris 9 (JDK
1.4.1_02) with the recommended J2SE patch applied
My code works fine if I Create a JVM and then proceed to invoke my Java code
from the env retruned by JNI_CreateJavaVM. However as soon as I deatach the
current thread from the Vm and then re-attach using AttachCurrentThread my
Java throws an exception as shown below
Exception in thread "Thread-0" javax.xml.parsers.FactoryConfigurationError:
Provider org.apache.xerces.jaxp.DocumentBuilderFactoryImpl not found
at
javax.xml.parsers.DocumentBuilderFactory.newInstance(DocumentBuilderFactory.
java:99)
Now nothing to do with classpaths or anything else has changed so why am I
getting an error just because I used AttachCurrentThread ?
If I replace the Java code with a very basic class that does nothing but
output to the console and calls some native functions everything works fine
so maybe it is something to do with the AttachCurrentThread and JAX but I'm
at a loss.
Exactly the same code works perfectly on Windows XP.
This is actually a simplification of the final problem which will be
attaching from multiple threads to the JVM to run the Java. By calling
DetachCurrentThread followed by AttachCurrentThread I am getting the same
error without the complication of the extra threads.
My code is summarized below (i've removed error checks for clarity). Can
anyone suggest a reason why this error occurs on Solaris.
TIA
Adrian
// The Global JVM Pointer
static JavaVM *theJVM = NULL;
JavaVM* nsvr_GetPointerToTheJVM()
{
if(theJVM != NULL)
{
return theJVM;
}
else
{
JNIEnv *env = NULL;
jint res;
JavaVMInitArgs vm_args;
JavaVMOption options[2];
options[0].optionString = "-Djava.class.path=" OCTOPUS_CLASSPATH;
options[1].optionString =
"-DOCTOPUS_HOME=/export/home/buildmc/testPas_Adrian/Octopus";
vm_args.version = 0x00010002;
vm_args.options = options;
vm_args.nOptions = 2;
vm_args.ignoreUnrecognized = JNI_TRUE;
res = JNI_CreateJavaVM(&theJVM, (void**)&env, &vm_args);
if (res < 0)
return NULL; // Can't create Java VM
//Comment the following line out and the Java runs fine
theJVM->DetachCurrentThread();
return theJVM;
}
}
int doOctopus(char* pOctoFilename)
{
JavaVM *jvm = NULL;
JNIEnv *env = NULL;
jint res, res2;
jclass cls;
jmethodID mid, mid2, mid3, clsCons;
jstring jstr, jstr2, jstr3;
jclass stringClass;
jobjectArray args, args2, args3;
jstring jStr4;
const char *str;
jobject clsInstance;
int rc = 0;
const JNINativeMethod methods[] = {
{
"publishProgress",
"(Ljava/lang/StringI",
&publishProgress__String
},
{
"publishProgress",
"(II)I",
&publishProgress__II
},
{
"publishProgress",
"(IIF)I",
&publishProgress__IIF
}
};
printf("In doOctopus()!\n");
jvm = nsvr_GetPointerToTheJVM();
if(jvm != NULL)
jvm->AttachCurrentThread((void **)&env, NULL);
if(jvm != NULL && env != NULL)
{
cls = (*env).FindClass("org/webdocwf/util/loader/Loader");
if (cls != 0)
{
res2 = env->RegisterNatives(cls, methods, 3); //Register Native
methods for progressFeedback
mid = (*env).GetStaticMethodID(cls, "main","([Ljava/lang/StringV");
jstr = (*env).NewStringUTF(pOctoFilename);
stringClass = (*env).FindClass("java/lang/String");
args = (*env).NewObjectArray(1, stringClass, jstr);
(*env).CallStaticVoidMethod( cls, mid, args);
}
if ((*env).ExceptionOccurred())
{
(*env).ExceptionDescribe();
}
if (cls != 0)
res2 = env->UnregisterNatives(cls);
res = jvm->DetachCurrentThread();
}
return 0;
}
int main(int argc, char* argv[])
{
char* octoFile1 =
"/export/home/buildmc/testPas_Adrian/Octopus/sInstoreJob.xml";
printf("C++ Hello from main()!\n");
int iRes = 0;
iRes = doOctopus(octoFile1);
return iRes;
}