Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problem creating JVM using JNI_CreateJavaVM for java 1.4 in hp unix

843829Dec 1 2004 — edited Jan 19 2005
Hello,

I have problem in creating a JVM from within my c code using the JNI_CreateJavaVM() in java 1.4. The program exits at the function call without returning..

I am attaching a test program, which is a modification of the invoke.c that comes in the sun JNI tutorial. This program is in the same lines as my main program. Please let me know if I am doing some obvious mistake. This program never returns after the call to create JVM. But I get the following message in the cmd line.

Error occurred during initialization of VM
Unable to load native library: specified library, or one of its dependencies, does not exist.


I have used the following cmd to compile :

cc -I/opt/java1.4/include -I/opt/java1.4/include/hp-ux -L/opt/java1.4/jre/lib/PA_RISC/server -ljvm test.c

The code is as follows :
--------------------------------
#include <jni.h>

#define PATH_SEPARATOR ':'
#define JAVA_CLASSPATH_OPT_12 "-Djava.class.path=%s"
#define JAVA_COMPILER_OPT_12 "-Djava.compiler=NONE"
#define JAVA_X_OPT_12 "-Xusealtsigs"

#define USER_CLASSPATH "." /* where Prog.class is */

main() {

JNIEnv *env;
JavaVM *jvm;
JavaVMInitArgs vm_args;
JavaVMOption VMOptions[3];
int optionsIndex = 0;

jint res;
jclass cls;
jmethodID mid;
jstring jstr;
jobjectArray args;
char classpath[1024];

/* Append USER_CLASSPATH to the end of default system class path */
sprintf(classpath, "%s%c%s", JAVA_CLASSPATH_OPT_12, PATH_SEPARATOR, USER_CLASSPATH);

VMOptions[optionsIndex].optionString = classpath;
optionsIndex++;
VMOptions[optionsIndex].optionString = (char*)JAVA_COMPILER_OPT_12;
optionsIndex++;
VMOptions[optionsIndex].optionString = (char*)JAVA_X_OPT_12;
optionsIndex++;



/* IMPORTANT: specify vm_args version # if you use JDK1.1.2 and beyond */
vm_args.version = 0x00010004;
vm_args.options = VMOptions;
vm_args.nOptions = optionsIndex;
vm_args.ignoreUnrecognized = JNI_FALSE;

printf("About to invoke JNI_CreateJavaVM\n");

/* Create the Java VM */
res = JNI_CreateJavaVM(&jvm,(void **)&env,&vm_args);

printf("res=%d\n",res);


if (res < 0) {
fprintf(stderr, "Can't create Java VM\n");
exit(1);
}

cls = (*env)->FindClass(env, "Prog");
if (cls == 0) {
fprintf(stderr, "Can't find Prog class\n");
exit(1);
}

mid = (*env)->GetStaticMethodID(env, cls, "main", "([Ljava/lang/String;)V");
if (mid == 0) {
fprintf(stderr, "Can't find Prog.main\n");
exit(1);
}

jstr = (*env)->NewStringUTF(env, " from C!");
if (jstr == 0) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
args = (*env)->NewObjectArray(env, 1,
(*env)->FindClass(env, "java/lang/String"), jstr);
if (args == 0) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
(*env)->CallStaticVoidMethod(env, cls, mid, args);

(*jvm)->DestroyJavaVM(jvm);
}



I would appreciate any help on this.

Thanks in advance.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 16 2005
Added on Dec 1 2004
5 comments
344 views