Undefined Link Try calling Java from C ++

I am trying to create a Java virtual machine from C ++ and call the main method that passes the String argument to the main method of the Java program. I follow this example found on the Sun website: http://java.sun.com/docs/books/jni/html/invoke.html#11202

Here is a simple Java program:

public class TestJNIInvoke { public static void main(String[] args) { System.out.println(args[0]); } } 

Here is C ++ - the program that I use (trying) to call the JVM:

 #include <jni.h> #include <cstdlib> using namespace std; int main() { JNIEnv *env; JavaVM *jvm; jint res; jclass cls; jmethodID mid; jstring jstr; jclass stringClass; jobjectArray args; JavaVMInitArgs vm_args; JavaVMOption* options = new JavaVMOption[1]; //LINE 18 ERROR options[0].optionString = (char*)&"-Djava.class.path=C:\\Program Files\\Java\\jdk1.7.0\\bin"; vm_args.version = JNI_VERSION_1_6; vm_args.nOptions = 1; vm_args.options = options; vm_args.ignoreUnrecognized = false; /* load and initialize a Java VM, return a JNI interface * pointer in env */ res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args); //LINE 26 ERROR if (res < 0) { fprintf(stderr, "Can't create Java VM\n"); exit(1); } cls = env->FindClass("TestJNIInvoke"); if (cls == NULL) { goto destroy; } mid = env->GetStaticMethodID(cls, "main", "([Ljava/lang/String;)V"); if (mid == NULL) { goto destroy; } jstr = env->NewStringUTF(" from CPP!"); if (jstr == NULL) { goto destroy; } stringClass = env->FindClass("java/lang/String"); args = env->NewObjectArray(1, stringClass, jstr); if (args == NULL) { goto destroy; } env->CallStaticVoidMethod(cls, mid, args); destroy: if (env->ExceptionOccurred()) { env->ExceptionDescribe(); } jvm->DestroyJavaVM(); } 

In any case, if I just compile the file with:

 gcc -I"c:\Program Files\Java\jdk1.7.0\include" -I"c:\Program Files\Java\jdk1.7.0\include\win32" -c TestJNIInvoke.cpp 

It compiles fine, but when I try to compile and link:

 gcc -I"c:\Program Files\Java\jdk1.7.0\include" -I"c:\Program Files\Java\jdk1.7.0\include\win32" -g TestJNIInvoke.cpp 

I get two strange errors that I don't understand:

 TestJNIInvoke.cpp:18: undefined reference to `operator new[](unsigned int)' TestJNIInvoke.cpp:26: undefined reference to ` _imp__JNI_CreateJavaVM@12 ' collect2: ld returned 1 exit status 

I have noted the lines in the above code where the error occurs, and has anyone encountered this problem before?

Any ideas / links would be nice

thanks

+4
source share
1 answer

First, do not use gcc. By default, the code that it processes is assumed to be written in C. When you want it to compile or link C ++ code, you must run g ++. This will lead to the emergence of standard C ++ headers and libraries.

Secondly, you need to enable java libraries. In section 7.2.1 on the linked page, you discuss this.

Your command line should look something like this:

 g++ -I"C:\Program Files\Java\jdk1.7.0\include" -L"C:\Program Files\Java\jdk1.7.0\lib" -lthread -ljava TestJNIInvoke.cpp 

Note that you may need to add additional include (-I) or linker (-L) directories.

+6
source

All Articles