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)

7
  • 1
    Looks like your code is not linking against the JNI library. How to do that depends on your build system. Commented Oct 25, 2015 at 21:53
  • Please post which platform, JDK, and build environment you use. Commented Oct 26, 2015 at 7: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) Commented Oct 26, 2015 at 16:22
  • Possible duplicate of undefined reference to `JNI_CreateJavaVM' linux Commented Oct 26, 2015 at 20:40
  • @AlexCohn That answer is for Linux. I am using Mac and can also use Windows. Commented Oct 26, 2015 at 22:07

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.