我目前正在尝试从C ++程序调用Java函数。我的编译器是g ++ 4.8.1。 我已经包含了jni.h并链接到libjvm.so文件,该文件是解析代码中所有符号所必需的。
我的错误是:
#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x00007f2f8c5f767a, pid=12471, tid=139842205632320
#
# JRE version: OpenJDK Runtime Environment (7.0_51) (build 1.7.0_51-b00)
# Java VM: OpenJDK 64-Bit Server VM (24.45-b08 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# V [libjvm.so+0x5c967a] get_method_id(JNIEnv_*, _jclass*, char const*, char const*, bool, Thread*) [clone .isra.80]+0x7a
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /home/bourgondaries/Development/RAIIGC/ide/codeblocks/hs_err_pid12471.log
#
# If you would like to submit a bug report, please include
# instructions on how to reproduce the bug and visit:
# http://icedtea.classpath.org/bugzilla
#
Aborted
我试图链接到不同的libjvm.so文件,但无济于事。 目前使用的是java-7openjdk-amd64。我也试过链接到java-gcj-4.8,java-1.5.0-gcj-4.8-amd64,java-6-openjdk-amd64。
#include <jni.h>
JNIEnv* create_vm()
{
JavaVM* jvm;
JNIEnv* env;
JavaVMInitArgs args;
JavaVMOption options[1];
args.version = JNI_VERSION_1_6;
args.nOptions = 1;
options[0].optionString = "-Djava.class.path=/home/bourgondaries/Development/";
args.options = options;
args.ignoreUnrecognized = JNI_FALSE;
JNI_CreateJavaVM(&jvm, (void **)&env, &args);
return env;
}
void invoke_class(JNIEnv* env)
{
jclass helloWorldClass;
jmethodID mainMethod;
jobjectArray applicationArgs;
jstring applicationArg0;
helloWorldClass = env->FindClass("Main");
mainMethod = env->GetStaticMethodID(helloWorldClass, "main", "([Ljava/lang/String;)V");
applicationArgs = env->NewObjectArray(1, env->FindClass("java/lang/String"), NULL);
applicationArg0 = env->NewStringUTF("From-C-program");
env->SetObjectArrayElement(applicationArgs, 0, applicationArg0);
env->CallStaticVoidMethod(helloWorldClass, mainMethod, applicationArgs);
}
int main ( int argc, char *argv[] )
{
JNIEnv* env = create_vm();
invoke_class( env );
}
这是java代码。
public class Main
{
public static void main ( String []args )
{
System.out.println("Hello World!");
}
}