【发布时间】:2019-09-27 14:12:52
【问题描述】:
我在 Eclipse 中为 JNI 使用本教程:
https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html#zz-2.6
(我只使用“2.6 JNI in Eclipse”部分)。
直到教程的这一部分:
运行目标“all”的makefile,右键单击makefile⇒Make>Targets⇒Build⇒选择目标“all”⇒Build
似乎一切正常(意思是 - 结果与教程中的相同,并且“问题”选项卡中没有警告或错误)。
但是当我做这部分时,我注意到这一行:
javah -classpath ../bin HelloJNI
在控制台的打印中丢失了。
然后我继续教程的下一步——“第 5 步:运行 Java JNI 程序”。
但即使它确实打印到控制台“Hello World!”,我注意到“问题”选项卡中有错误:
"make: *** 没有规则可以将目标设为 `all'。停止。"
开发环境
+面向 Java 开发人员的 Eclipse IDE(32 位) 版本:Kepler Service Release 2。
+Eclipse 的 CDT 插件
+Windows 10 64 位(我使用 eclipse 32 位,因为在某些时候,64 位 eclipse 无法打开,解决方案是使用 32 位 eclipse)
制作文件
# Define a variable for classpath
CLASS_PATH = ../bin
# Define a virtual path for .class in the bin directory
vpath %.class $(CLASS_PATH)
all : hello.dll
# $@ matches the target, $< matches the first dependency
hello.dll : HelloJNI.o
gcc -Wl,--add-stdcall-alias -shared -o $@ $<
# $@ matches the target, $< matches the first dependency
HelloJNI.o : HelloJNI.c HelloJNI.h
gcc -I"C:\Program Files (x86)\Java\jdk1.8.0_212\include" -I"C:\Program Files (x86)\Java\jdk1.8.0_212\include\win32" -c $< -o $@
# $* matches the target filename without the extension
HelloJNI.h : HelloJNI.class
javah -classpath $(CLASS_PATH) $*
clean :
rm HelloJNI.h HelloJNI.o hello.dll
HelloJNI.c
#include <jni.h>
#include <stdio.h>
#include "HelloJNI.h"
JNIEXPORT void JNICALL Java_HelloJNI_sayHello(JNIEnv *env, jobject thisObj) {
printf("Hello World!\n");
return;
}
HelloJNI.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class HelloJNI */
#ifndef _Included_HelloJNI
#define _Included_HelloJNI
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: HelloJNI
* Method: sayHello
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_HelloJNI_sayHello
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
HelloJNI.java
public class HelloJNI {
static {
System.loadLibrary("hello"); // hello.dll (Windows) or libhello.so (Unixes)
}
// Declare native method
private native void sayHello();
// Test Driver
public static void main(String[] args) {
new HelloJNI().sayHello(); // Allocate an instance and invoke the native method
}
}
【问题讨论】:
-
我认为我可能会收到该错误,因为可能 Eclipse 正在项目文件夹中搜索我的 makefile,而不是项目文件夹内的“jni”文件夹。不过就算是这个原因,我也不知道怎么解决。