Note that there are some explanatory texts on larger screens.

plurals
  1. POUndefined Reference Trying to invoke Java from C++
    text
    copied!<p>I am trying to create a Java virtual machine from C++ and invoke the main method passing a String argument to the main method of the Java program. I am following this example found on Sun's website: <a href="http://java.sun.com/docs/books/jni/html/invoke.html#11202" rel="nofollow">http://java.sun.com/docs/books/jni/html/invoke.html#11202</a></p> <p>Here is the simple Java Program:</p> <pre><code>public class TestJNIInvoke { public static void main(String[] args) { System.out.println(args[0]); } } </code></pre> <p>Here is the C++ program I am using to (try to) invoke the JVM:</p> <pre><code>#include &lt;jni.h&gt; #include &lt;cstdlib&gt; 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*)&amp;"-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(&amp;jvm, (void**)&amp;env, &amp;vm_args); //LINE 26 ERROR if (res &lt; 0) { fprintf(stderr, "Can't create Java VM\n"); exit(1); } cls = env-&gt;FindClass("TestJNIInvoke"); if (cls == NULL) { goto destroy; } mid = env-&gt;GetStaticMethodID(cls, "main", "([Ljava/lang/String;)V"); if (mid == NULL) { goto destroy; } jstr = env-&gt;NewStringUTF(" from CPP!"); if (jstr == NULL) { goto destroy; } stringClass = env-&gt;FindClass("java/lang/String"); args = env-&gt;NewObjectArray(1, stringClass, jstr); if (args == NULL) { goto destroy; } env-&gt;CallStaticVoidMethod(cls, mid, args); destroy: if (env-&gt;ExceptionOccurred()) { env-&gt;ExceptionDescribe(); } jvm-&gt;DestroyJavaVM(); } </code></pre> <p>Anyway If I just compile the file with:</p> <pre><code>gcc -I"c:\Program Files\Java\jdk1.7.0\include" -I"c:\Program Files\Java\jdk1.7.0\include\win32" -c TestJNIInvoke.cpp </code></pre> <p>It compiles fine, but when I try to compile and link:</p> <pre><code>gcc -I"c:\Program Files\Java\jdk1.7.0\include" -I"c:\Program Files\Java\jdk1.7.0\include\win32" -g TestJNIInvoke.cpp </code></pre> <p>I get two weird errors that I don't understand:</p> <pre><code>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 </code></pre> <p>I marked the lines in the above code where the error is occuring, has anyone encountered this problem before? </p> <p>Any ideas/links would be great</p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload