3

I need to run Java from C++, and in general that problem is solved, but something is wrong with my make system or scripts.

I have a C++ file that creates the JVM:

#include <jni.h>
#include <iostream>
#include <string>

using namespace std;

int main() {
    JavaVMOption jvmopt[1];
    jvmopt[0].optionString = const_cast<char *>("-Djava.class.path=.");

    JavaVMInitArgs vmArgs;
    vmArgs.version = JNI_VERSION_1_2;
    vmArgs.nOptions = 1;
    vmArgs.options = jvmopt;
    vmArgs.ignoreUnrecognized = JNI_TRUE;

    // Create the JVM
    JavaVM *javaVM;
    JNIEnv *jniEnv;
    long flag = JNI_CreateJavaVM(&javaVM, (void **)
            &jniEnv, &vmArgs);

    return 0;
}

And I have a CMakeLists.txt file:

cmake_minimum_required(VERSION 3.12)
project(games_test_system)

include_directories(/usr/java/jdk-10.0.2/include /usr/java/jdk-10.0.2/include/linux)
link_directories(/usr/java/jdk-10.0.2/lib /usr/java/jdk-10.0.2/lib/server)

set(CMAKE_CXX_STANDARD 14)

add_executable(games_test_system main.cpp)

I run it with:

/opt/clion-2018.2.2/bin/cmake/linux/bin/cmake --build /home/obabichev/CLionProjects/games-test-system/cmake-build-debug --target games_test_system -- -j 4

But I get this error:

[ 50%] Linking CXX executable games_test_system
CMakeFiles/games_test_system.dir/main.cpp.o: In function `main':
/home/obabichev/CLionProjects/games-test-system/main.cpp:21: undefined reference to `JNI_CreateJavaVM'
collect2: error: ld returned 1 exit status
CMakeFiles/games_test_system.dir/build.make:83: recipe for target 'games_test_system' failed
make[3]: *** [games_test_system] Error 1
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/games_test_system.dir/all' failed
make[2]: *** [CMakeFiles/games_test_system.dir/all] Error 2
CMakeFiles/Makefile2:84: recipe for target 'CMakeFiles/games_test_system.dir/rule' failed
make[1]: *** [CMakeFiles/games_test_system.dir/rule] Error 2
Makefile:118: recipe for target 'games_test_system' failed
make: *** [games_test_system] Error 2

I feel that the problem is with the linker, but I do not understand what specifically the problem is.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    Are you sure you need `/lib/server` as link directory? On my machine the `jvm.lib` file is just in `/lib`. Can you check there is a `jvm.a` file in that directory? – Jorn Vernee Aug 23 '18 at 13:04
  • without '/usr/java/jdk-10.0.2/lib/server' I have error ' cannot find -ljvm', and I have no file jvm.a in the jdk directory... Might it be because I have jdk 10? I will try install jdk 8 – Oleg Babichev Aug 23 '18 at 13:11
  • with jdk8 the same problem... cmake_minimum_required(VERSION 3.12) project(games_test_system) include_directories(/usr/java/jdk1.8.0_181-amd64/include /usr/java/jdk1.8.0_181-amd64/include/linux) link_directories(/usr/java/jdk1.8.0_181-amd64/lib ) set(CMAKE_CXX_STANDARD 14) add_executable(games_test_system main.cpp) – Oleg Babichev Aug 23 '18 at 13:23

1 Answers1

2

In your CMakeLists.txt file, add

find_package(JNI REQUIRED)
target_link_libraries(games_test_system ${JNI_LIBRARIES})

instead of choosing the directories manually, you can also rely on CMake:

include_directories(${JNI_INCLUDE_DIRS}
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307