0

I am trying to call classes in Java from C++ using jni.h. After some research, I used the following code:

JavaVM *jvm;
JNIEnv *env;

JavaVMInitArgs vm_args;
JavaVMOption* options = new JavaVMOption[1];

options[0].optionString = "-Djava.class.path=/usr/lib/java";
vm_args.version = JNI_VERSION_1_6;
vm_args.nOptions = 1;
vm_args.options = options;
vm_args.ignoreUnrecognized = false;

JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
delete options;

jclass cls = env->FindClass("Test.java");
jmethodID mid = env->GetStaticMethodID(cls, "test", "(I)V");
env->CallStaticVoidMethod(cls, mid, 100);

jvm->DestroyJavaVM();

However, I am getting 2 errors and they are as follows:

  1. "_JNI_CreateJavaVM", referenced from: _main in main.o"

  2. ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

Does anyone have any input on how to resolve these issues? Thank you very much.

Java version: 1.7.0_71-b14 Java (TM) SE Runtime Environment: (build 1.7.0_71-b14) Java HotSpot(TM) 64-bit Server VM (build 24.71-b01, mixed mode)

Francis
  • 73
  • 1
  • 1
  • 6
  • 1
    Looks like your code is not linking against the JNI library. How to do that depends on your build system. – znkr Oct 25 '15 at 21:53
  • Please post which platform, JDK, and build environment you use. – Alex Cohn Oct 26 '15 at 07:10
  • @AlexCohn Java version: 1.7.0_71-b14 Java (TM) SE Runtime Environment (build 1.7.0_71-b14) Java HotSpot(TM) 64-bit Server VM (build 24.71-b01, mixed mode) – Francis Oct 26 '15 at 16:22
  • Possible duplicate of [undefined reference to \`JNI\_CreateJavaVM' linux](http://stackoverflow.com/questions/16860021/undefined-reference-to-jni-createjavavm-linux) – Alex Cohn Oct 26 '15 at 20:40
  • @AlexCohn That answer is for Linux. I am using Mac and can also use Windows. – Francis Oct 26 '15 at 22:07
  • Have you seen the Windows discussion [undefined reference to 'JNI_CreateJavaVM' windows](http://stackoverflow.com/questions/16930567/undefined-reference-to-jni-createjavavm-windows)? – Alex Cohn Oct 27 '15 at 06:11
  • The bottom line is, you need to give `-ljvm` to your linker, and make sure that the linker finds the correct library. – Alex Cohn Oct 27 '15 at 06:18

2 Answers2

0

If you have the appropriate shared object libraries (.so on Linux, .dll on Windows) check that you are using the same architecture between your compiler configuration and your set of shared libraries.

If you aren't referencing said shared object libraries at the linking phase, you will need to do so.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
0

I managed to figure out what I had to do. I am working on my MacBook Pro and using XCode. I did the following:

  1. On the left-hand panel, I selected the project I am working on.
  2. I clicked 'Build Phases' in the center of the screen.
  3. I opened the 'Link Binary with Libraries' tab.
  4. I hit the '+' sign and added the 'JavaVM.framework' file.

That worked for me.

Francis
  • 73
  • 1
  • 1
  • 6