gpt4 book ai didi

java - JNI 演示 HelloWorld

转载 作者:太空宇宙 更新时间:2023-11-04 00:02:00 26 4
gpt4 key购买 nike

这是我的第一个JNI demo。我的操作系统是win7,我的ide是IDEA。

创建 Java 文件 HelloWorld

HelloWold.java:

public class HelloWorld {
public static native void sayHello();

public static void main(String[] args) {
new HelloWorld().sayHello();
}

static {
System.load("D:\\JavaJNIDemo\\jni\\helloworld.dll");
}

}

Javah生成.h文件

com_uniquestudio_coxier_HelloWorld.h:

#include <jni.h>
#ifndef _Included_com_uniquestudio_coxier_HelloWorld
#define _Included_com_uniquestudio_coxier_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_uniquestudio_coxier_HelloWorld
* Method: sayHello
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_uniquestudio_coxier_HelloWorld_sayHello
(JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif
#endif

新建一个c文件

HelloWorld.c:

#include <jni.h>
#include <stdio.h>
#include "com_uniquestudio_coxier_HelloWorld.h"


JNIEXPORT void JNICALL Java_com_uniquestudio_coxier_HelloWorld_sayHello
(JNIEnv *env, jclass object){
printf("Hello World!\n");
return;
}

在我的win7上生成dll

我遇到了一个问题:

unknown type name "__int64" in jni_md.h

我通过 this 解决了它

然后我使用:

D:\JavaJNIDemo\jni>gcc -Wl,--add-stdcall-alias -I"%JAVA_HOME%\include" -I"%JAVA_
HOME%\include\win32" -shared -o helloworld.dll HelloWorld.c

错误

当我运行这个程序时,我得到这个错误:错误:

A fatal error has been detected by the Java Runtime Environment:

EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000000180105979, pid=6776, tid=7040

JRE version: Java(TM) SE Runtime Environment (8.0_20-b26) (build 1.8.0_20-b26)
Java VM: Java HotSpot(TM) 64-Bit Server VM (25.20-b23 mixed mode windows-amd64 compressed oops)
Problematic frame:
C [cygwin1.dll+0xc5979]

Failed to write core dump. Minidumps are not enabled by default on client versions of Windows

An error report file with more information is saved as:
D:\JavaJNIDemo\hs_err_pid6776.log

最佳答案

我将编译器更改为 MinGW64 .然后就可以了

关于java - JNI 演示 HelloWorld,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39453904/

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