JNI API调用导致JNI_EVERSION错误

时间:2018-12-19 09:49:06

标签: java c java-native-interface

我无法在我的OS上运行Java本机接口(JNI)。我的最小工作示例(源自here,并针对我的系统进行了调整),在调用JNI_CreateJavaVM()时返回版本错误 JNI_EVERSION(-3)。我的c文件看起来像

// hello_world.c
#include <stdio.h>
#include <jni.h>

JNIEnv *create_vm(JavaVM **jvm)
{
  JNIEnv* env;
  JavaVMInitArgs args;
  JavaVMOption options;
  args.version = JNI_VERSION_1_8;
  args.nOptions = 1;
  options.optionString = "-Djava.class.path=./";
  args.options = &options;
  args.ignoreUnrecognized = 0;
  int rv;
  rv = JNI_CreateJavaVM(jvm, (void**)&env, &args);
  if (rv < 0 || !env)
      printf("Unable to Launch JVM %d\n",rv);
  else
      printf("Launched JVM! :)\n");
  return env;
}

int main(int argc, char *argv[])
{
  JavaVM *jvm;
  JNIEnv *env;
  env = create_vm(&jvm);
  return 0;
}

和我的Makefile一样

all: run

helloWorld.class: helloWorld.java
    /usr/bin/javac helloWorld.java

hello_world: hello_world.c
    gcc -o hello_world \
    -L /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/server/ \
    -ljvm \
    -I /usr/lib/jvm/java-8-openjdk-amd64/include/ \
    -I /usr/lib/jvm/java-8-openjdk-amd64/include/linux/ \
    hello_world.c

run: helloWorld.class hello_world
    export LD_LIBRARY_PATH="/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/server/"
    ./hello_world

clean:
    rm -f helloWorld.class hello_world

make的输出结果是

/usr/bin/javac helloWorld.java
gcc -o hello_world \
-L /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/server/ \
-ljvm \
-I /usr/lib/jvm/java-8-openjdk-amd64/include/ \
-I /usr/lib/jvm/java-8-openjdk-amd64/include/linux/ \
hello_world.c
export LD_LIBRARY_PATH="/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/server/"
./hello_world
./hello_world: /usr/lib/jvm/java-1.5.0-gcj-6-amd64/lib/libjvm.so: no version information available (required by ./hello_world)
Unable to Launch JVM -3

消息“无法启动JVM -3”是带有JNI_EVERSION错误的程序输出。我认为行/usr/lib/jvm/java-1.5.0-gcj-6-amd64/lib/libjvm.so: no version information available (required by ./hello_world)可以提示问题,但我不太了解这个问题,当我使用readelf -h /usr/lib/jvm/java-1.5.0-gcj-6-amd64/lib/libjvm.so查看共享库时,它会显示一行

 Version:                           1 (current)

1 个答案:

答案 0 :(得分:0)

我终于能够运行它。即使我不知道为什么,export LD_LIBRARY_PATH中的Makefile命令显然也没有执行它的工作。在命令行上检查LD_LIBRARY_PATH的内容后,我注意到其中包含

/usr/lib/jvm/java-1.5.0-gcj-6-amd64/lib

设置后

LD_LIBRARY_PATH="/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/server/"

并重新运行make,我能够启动JVM