gpt4 book ai didi

java - JNI 无法识别 jni 原始类型,例如字符串

转载 作者:太空宇宙 更新时间:2023-11-03 23:44:37 28 4
gpt4 key购买 nike

我已经为 C 头文件制作了一个 make 文件,它工作正常,但是说 JNICALLJNIEnv 存在语法错误,但我已经弄明白了这是因为头文件中的类型。

Image of the failure

图中的代码在这里:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class GameLogic */

#ifndef _Included_GameLogic
#define _Included_GameLogic
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: GameLogic
* Method: hello
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_GameLogic_hello
(JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

当我尝试运行该项目时出现错误:

Exception in thread "main" java.lang.UnsatisfiedLinkError: GameLogic.hello()Ljava/lang/String;
at GameLogic.hello(Native Method)
at GameLogic.<init>(GameLogic.java:7)
at GameLogic.main(GameLogic.java:11)

C文件:

#import <jni.h>
#import <stdio.h>
#include "GameLogic.h"

JNIEXPORT jstring JNICALL Java_GameLogic_hello (JNIEnv * env, jobject thisObj) {
return (*env)->NewStringUTF(env, "Sup");
}

通用生成文件:

# Define a variable for classpath
CLASS_PATH = ../

# Define a virtual path for .class in the bin directory
vpath %.class $(CLASS_PATH)

all : libGameLogic.jnilib

# $@ matches the target, $< matches the first dependancy
libGameLogic.jnilib : GameLogic.o
gcc -dynamiclib -framework JavaVM -o $@ $<

# $@ matches the target, $< matches the first dependancy
GameLogic.o : GameLogic.c GameLogic.h
gcc -I/System/Library/Frameworks/JavaVM.framework/Headers -framework JavaVM -c $< -o $@

# $* matches the target filename without the extension
GameLogic.h : GameLogic.class
javah -classpath $(CLASS_PATH) $*

clean :
rm GameLogic.h GameLogic.o libGameLogic.jnilib

游戏逻辑:

public class GameLogic {

public native String hello ();

public GameLogic () {
System.out.println(hello());
}

public static void main (String[] args) {
new GameLogic ();
}

}

最佳答案

尝试编译使用:

gcc -I"$JAVA_HOME/include" -I"$JAVA_HOME/include/darwin/" -dynamiclib -o libGameLogic.jnilib GameLogic.c

然后在你的类文件中你需要添加:

static {
// GameLogic.dll (Windows) or libGameLogic.so (Unixes) or libGameLogic.jnilib (Mac)
System.loadLibrary("GameLogic");
}

最后你启动你的类作为下一个 (假设你的文件 .class 和你的 libGameLogic.jnilib 在当前目录中):

java -Djava.library.path=. GameLogic

关于java - JNI 无法识别 jni 原始类型,例如字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36847103/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com