简体   繁体   English

包中带有.java文件的Eclipse JNI

[英]Eclipse JNI with .java file in package

I am writing a simple demo JNI project in Eclipse to integrate Java and C code. 我正在Eclipse中编写一个简单的演示JNI项目,以集成Java和C代码。 I have installed the CDT plugin for Eclipse to do this. 我已经为Eclipse安装了CDT插件来做到这一点。 With this project structure I have my HelloJNI java file inside the '(default package)' of Eclipse and have no problems using the makefile to generate a HelloJNI.h C header file. 通过这种项目结构,我将HelloJNI java文件放在Eclipse的“(默认包)”中,并且使用makefile生成HelloJNI.h C头文件没有问题。

My Makefile: 我的Makefile:

# Define a variable for classpath
CLASS_PATH = ../bin

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

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

The problem is that I cannot figure out how to get this to work with HelloJNI.java being inside a package such as com.example instead of the default package. 问题是,我无法弄清楚如何得到这个与HelloJNI.java包内是如com。示例 ,而不是默认包工作。 ie this structure . 这种结构 When running the same make target I get the error output: 当运行相同的make目标时,我得到错误输出:

make: *** No rule to make target 'HelloJNI.class', needed by 'HelloJNI.h'. make:***没有规则来创建目标“ HelloJNI.class”,这是“ HelloJNI.h”所需的。 Stop. 停止。

I attempted to add the package name to the javah command: 我试图将包名称添加到javah命令中:

javah -classpath $(CLASS_PATH) com.example.HelloJNI

...but get the same error. ...但是得到同样的错误。

I attempted to change the classpath to: 我试图将类路径更改为:

CLASS_PATH = ../bin/com/example

...but get the following error: ...但是出现以下错误:

make HelloJNI.h 制作HelloJNI.h

javah -classpath ../bin/com/example HelloJNI javah -classpath ../bin/com/example HelloJNI

Error: Could not find class file for 'HelloJNI'. 错误:找不到“ HelloJNI”的类文件。

What do I need to do with my makefile to make this work? 为了使这项工作有效,我需要对我的makefile做些什么?

The javah utility expects you to give it the fully-qualified name of the class you want it to analyze, and it expects to find that class relative to the classpath, according to that name. javah实用工具希望您为其提供要分析的类的全限定名称,并且期望根据该名称找到相对于类路径的该类。 Thus, if the name of the class is com.example.HelloJNI , then you might tell make something like this: 因此,如果类的名称是com.example.HelloJNI ,那么你可能会告诉make这样的事情:

CLASS_DIR = ../bin

HelloJNI.h : $(CLASS_DIR)/com/example/HelloJNI.class
    javah -force -classpath $(CLASS_DIR) com.example.HelloJNI

You can dress that up a bit if you want. 您可以根据需要打扮一下。 I use something a little DRYer and with a bit more automation in my own Makefiles, but I'm trying to get across the key points. 我在自己的Makefile文件中使用了一些DRYer和更多的自动化功能,但是我试图了解关键点。 In particular: 尤其是:

  1. Express the correct prerequisite file (the .class file, in the correct directory). 表达正确的先决条件文件(.class文件,在正确的目录中)。 If you do not do this correctly then you will end up having your header sometimes being rebuilt when it does not need to be and / or not being updated when it does need to be. 如果你不这样做正确,那么你将最终有你的头,有时在重建时,它并不需要是和/或当需要是被更新。

  2. Specify an appropriate -classpath to javah ; 指定javah的适当的-classpath this follows the normal Java rules for expressing class paths. 这遵循用于表达类路径的常规Java规则。

  3. Specify the correct, fully-qualified class name to javah . javah指定正确的标准类名。

  4. Use the -force option, else javah will refuse to replace headers that already exist. 使用-force选项,否则javah将拒绝替换已经存在的标头。 That default behavior is reasonable when you invoke javah manually, but it will mess you up when you are trying to use make to keep the JNI headers up to date. 当您手动调用javah时,该默认行为是合理的,但是当您尝试使用make保持JNI标头为最新时,它将使您感到混乱。 Of course, it follows that you must not make manual modifications to those headers, because such modifications are likely to be clobbered. 当然,因此您不能对这些标头进行手动修改,因为这样的修改很可能被破坏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM