当我在大学学习CS时,我们不得不用C建立一个CBIR项目(并且我们得到了完成图像处理所有“艰苦工作”的C ++文件)。
现在,我正在尝试用Java创建此项目。
我不知道如何用Java实现该C ++文件中的某些内容,因此我决定使用JNI并从Java程序中调用该文件。
我正在将本教程用于JNI: https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html#zz-2.6
+面向Java开发人员的Eclipse IDE(32位),版本:2018-09(4.9.0)。
+ Eclipse的CDT插件
+ Windows 10 64位(我使用eclipse 32位,因为有时无法打开64位eclipse,解决方案是使用32位eclipse)
当我完成本教程的这一部分时:
右键单击“ makefile”⇒创建目标⇒创建⇒在“目标名称”中,输入“全部”。重复以创建目标“干净”。
通过右键单击makefile来运行目标“所有”的makefile⇒生成>目标⇒构建⇒选择目标“ all”⇒构建
似乎运行良好。
但是,当我运行该程序时(如“第5步:运行Java JNI程序”中的建议),尽管该程序在控制台上打印了我想要的内容,但在完成运行后,Eclipse向我展示了:
Unresolved inclusion: <jni.h>
在c ++文件和头文件中均。
我检查了一下,在makefile中写的路径中确实有jni.h文件(“ C:\ Program Files(x86)\ Java \ jdk1.8.0_212 \ include“)。
此外,我想在下一次更改cpp / h文件后知道如何编译。
现在我注意到在运行程序之前,“问题”标签中有一个警告:
无效的项目路径:找不到包含路径(程序文件>(x86)\ Java \ jdk1.8.0_212 \ include)。
但是我不明白为什么将其写为“ Program Files”而不是“ C \ Program Files”。
我创建了一个新项目,希望它将解决问题。 当我对目标“全部”进行“构建”时,出现错误:
make: *** No rule to make target `all'. Stop.
我看到有人建议检查“自动生成Makefile”(在项目属性-> C / C ++ Build中),但是当我这样做时,我在控制台上看到“没有为项目CBIR2进行构建”
所以我再次“建立”了目标“全部”,并且以某种“神奇”的方式消失了。
然后看起来运行正常(即使运行时间很长),但是当我检查“问题”选项卡时,我再次看到错误:
make: *** No rule to make target `all'. Stop.
因此,我再次“构建”目标“ all”,该错误消失了。
但是,如果我每次运行程序时以及第一次运行程序之前都收到该错误,显然这里是有问题的。
# Define a variable for classpath
CLASS_PATH = ../bin
# Define a virtual path for .class in the bin directory
vpath %.class $(CLASS_PATH)
all : SPImageProc.dll
# $@ matches the target, $< matches the first dependency
SPImageProc.dll : SPImageProc.o
gcc -Wl,--add-stdcall-alias -shared -o $@ $<
# $@ matches the target, $< matches the first dependency
SPImageProc.o : SPImageProc.cpp SPImageProc.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
SPImageProc.h : SPImageProc.class
javah -classpath $(CLASS_PATH) $*
clean :
rm SPImageProc.h SPImageProc.o SPImageProc.dll
#include <jni.h>
#include <stdio.h>
#include "SPImageProc.h"
JNIEXPORT void JNICALL Java_SPImageProc_saySPImageProc(JNIEnv *env, jobject thisObj) {
printf("SPImageProc 333!\n");
return;
}
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class SPImageProc */
#ifndef _Included_SPImageProc
#define _Included_SPImageProc
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: SPImageProc
* Method: saySPImageProc
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_SPImageProc_saySPImageProc
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
public class CBIR {
public static void main(String[] args) {
SPImageProc.example();
}
}
public class SPImageProc {
static {
System.loadLibrary("SPImageProc"); // SPImageProc.dll
}
// Declare native method
private native void saySPImageProc();
public static void example() {
// Allocate an instance and invoke the native method
new SPImageProc().saySPImageProc();
}
}