I tried to compile the sample code at this link:
http://java.sun.com/docs/books/tutorial/native1.1/invoking/invo.html
I am using MS Visual C++ 6.0,
set the include path to:
D:\app\jdk13\include,D:\app\jdk13\include\win32
set the lib directory to:
D:\app\jdk13\lib
in the invoke.c file I make sure that PATH_SEPARATOR is defined correctly:
#define PATH_SEPARATOR ';'
However when I run the program I get an error code -3 returned when the
following line is performed:
res = JNI_CreateJavaVM(&jvm,&env,&vm_args);
Is there any documentation for these kind of error codes around?
Or has anyone had the same problem, and give me some hint??
However there are two versions provided with j2sdk.
--
Mojo <nano...@gmx.de> schrieb in im Newsbeitrag:
9c6dqs$c6tud$1...@ID-61914.news.dfncis.de...
Now the JNI_GetDefaultJavaVMInitArgs does not initialize the vm_args
structure correctly. So I initialized every member manually.
If I do this JNI_CreateJavaVM returns -1 according to jni.h this is an
"unknown error". GREAT!!!
Somehow I get the feeling I am moving in a circle.
I think I'll download JDK1.1;
BTW has anyone a modified version of this kind of sample that works with
Java2SDK1.3 ???
I also tried to compile the file as a CPP file but the compiler hardly
recognises any member.
If anybody could give me a version that has been modified to compile as CPP
file, I think I would appreciate it!!
--
Mojo <nano...@gmx.de> schrieb in im Newsbeitrag:
9c6e1h$c6120$1...@ID-61914.news.dfncis.de...
Correct.
>
> Now the JNI_GetDefaultJavaVMInitArgs does not initialize the vm_args
> structure correctly. So I initialized every member manually.
JNI changed a bit between JDK 1.1.x and JDK 1.2.
http://java.sun.com/j2se/1.3/docs/guide/jni/jni-12.html
> If I do this JNI_CreateJavaVM returns -1 according to jni.h this is an
> "unknown error". GREAT!!!
>
> Somehow I get the feeling I am moving in a circle.
>
> I think I'll download JDK1.1;
> BTW has anyone a modified version of this kind of sample that works with
> Java2SDK1.3 ???
Yes.
>
> I also tried to compile the file as a CPP file but the compiler hardly
> recognises any member.
Yes, because the syntax for JNI calls is different for C than C++.
> If anybody could give me a version that has been modified to compile as CPP
> file, I think I would appreciate it!!
Here is a simple C++ example:
//-----------------C++ code-----------------
#include <jni.h>
#include <stdio.h>
int main() {
JavaVM *vm;
JNIEnv *env;
JavaVMInitArgs vm_args;
JavaVMOption options[1];
options[0].optionString = "-Djava.class.path=.";
//other common options:
//options[1].optionString = "-Djava.compiler=NONE";
//options[2].optionString = "-Djava.library.path=.";
//options[3].optionString = "-verbose:jni";
vm_args.version = JNI_VERSION_1_2;
vm_args.options = options;
vm_args.nOptions = 1;
vm_args.ignoreUnrecognized = JNI_FALSE; //helpful sometimes
jint res = JNI_CreateJavaVM(&vm, (void **)&env, &vm_args);
if (res < 0) {
printf("Can't create Java VM\n");
exit(1);
}
//get the HelloWorld class
jclass cls = env->FindClass("HelloWorld");
if (cls == 0) {
printf("Can't find HelloWorld class\n");
exit(1);
}
//get the main() method
jmethodID mid = env->GetStaticMethodID(cls, "main",
"([Ljava/lang/String;)V");
if (mid == 0) {
printf("Can't find main() method\n");
exit(1);
}
//create an array for command line arguments expected by main()
strClass = env->FindClass("java/lang/String");
jobjectArray args = env->NewObjectArray(1, strClass, NULL);
//call main()
env->CallStaticVoidMethod(cls, mid, args);
}
//----------------Java code-----------------------
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Jim S.