这是我的第一个演示。我的操作系统是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文件
CoxOnEngestudiooCyxelyHeloLord.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

新建AC文件
地狱世界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
我遇到了一个问题:
JNI中未知的类型名“\u int64”
我通过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。然后它就可以工作了

10-08 14:07