使用JNI调用从Cpp返回字符串数组的Java方法
我试图从C调用以下java方法++使用JNI调用从Cpp返回字符串数组的Java方法,java,c++,java-native-interface,Java,C++,Java Native Interface,我试图从C调用以下java方法++ final String[]games=GameLoader.listGames() 其中,GameLoader与import player.GameLoader一起导入。GameLoader类存在于jar文件中。我尝试使用JNI从C++加载JAR文件,然后调用上面的方法。下面是我尝试从./p>中扩展的C++代码。 我认为该错误与CallObjectMethod调用有关,但我不确定。 我也在使用Java8 编辑: 看起来mid的值为0,这意味着找不到该方法。我
final String[]games=GameLoader.listGames()代码>
其中,GameLoader
与import player.GameLoader一起导入代码>。GameLoader
类存在于jar文件中。我尝试使用JNI从C++加载JAR文件,然后调用上面的方法。下面是我尝试从./p>中扩展的C++代码。
我认为该错误与CallObjectMethod
调用有关,但我不确定。
我也在使用Java8
编辑:
看起来mid
的值为0,这意味着找不到该方法。我想这就是问题所在,但是我不确定为什么没有找到它。这一点已通过检查
int main() {
initJVM();
jclass gameLoader = env->FindClass("player/GameLoader");
//final String[] games = GameLoader.listGames();
if(gameLoader == NULL)
{
cout << "Could not load class!" << endl;
return 1;
}
jmethodID mid = env->GetMethodID(gameLoader,"listGames","()[Ljava/lang/String;");
if(mid == NULL)
{
cout << "Could not load method!" << endl;
return 1;
}
jobjectArray stringArray = (jobjectArray) env->CallObjectMethod(gameLoader,mid);
if(stringArray == NULL)
{
cout << "Could not load object!" << endl;
return 1;
}
closeJVM();
}
intmain(){
initJVM();
jclass gameLoader=env->FindClass(“玩家/游戏加载器”);
//最终字符串[]games=GameLoader.listGames();
如果(gameLoader==NULL)
{
无法检查查找的结果class@geckos我会打印it@user1893354--您确实应该在代码中验证是否找到了类方法,并且在没有找到值的证明的情况下不要声明。您应该检查所有返回值,包括FindClass
GameLoader.listGames()的返回;
使它看起来像是listGames
是一个静态方法。在这种情况下,您应该使用GetStaticMethodID
而不是GetMethodID
。同样,您应该使用CallStaticObjectMethod
而不是CallObjectMethod
。
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x00007f0e835619d7, pid=3504, tid=0x00007f0e84028740
#
# JRE version: OpenJDK Runtime Environment (8.0_222-b10) (build 1.8.0_222-8u222-b10-1ubuntu1~16.04.1-b10)
# Java VM: OpenJDK 64-Bit Server VM (25.222-b10 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# V [libjvm.so+0x6849d7]
#
# 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/alex/jni_examples/hs_err_pid3504.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
#
Aborted (core dumped)
int main() {
initJVM();
jclass gameLoader = env->FindClass("player/GameLoader");
//final String[] games = GameLoader.listGames();
if(gameLoader == NULL)
{
cout << "Could not load class!" << endl;
return 1;
}
jmethodID mid = env->GetMethodID(gameLoader,"listGames","()[Ljava/lang/String;");
if(mid == NULL)
{
cout << "Could not load method!" << endl;
return 1;
}
jobjectArray stringArray = (jobjectArray) env->CallObjectMethod(gameLoader,mid);
if(stringArray == NULL)
{
cout << "Could not load object!" << endl;
return 1;
}
closeJVM();
}