简体   繁体   English

Java,JNI和C ++:如何从本机方法声明生成头文件?

[英]Java, JNI and C++: How do I generate a header file from native method declarations?

Java, JNI and C++: How do I generate a header file from native method declarations? Java,JNI和C ++:如何从本机方法声明生成头文件?

I have Java project, which communicates with C++ code through JNI. 我有一个Java项目,该项目通过JNI与C ++代码进行通信。 The challenge is now I need to add new methods. 现在的挑战是我需要添加新方法。 I first started by declaring the native methods in the java code. 我首先从在Java代码中声明本机方法开始。 Now I need to regenerate the header file for the JNI methods. 现在,我需要重新生成JNI方法的头文件。 I work in Eclipse, not sure how to do this. 我在Eclipse中工作,不确定如何执行此操作。

This is not built into Eclipse but it is easy to do: 这不是Eclipse内置的,但是很容易做到:

  1. Go File » New... » XML File to create an Ant file (code below) in your project. 转到File » New... » XML File以在项目中创建一个Ant文件(下面的代码)。
  2. Go Project » Properties » Builders » New... » Ant Builder to add the Ant file as a build step. 转到Project » Properties » Builders » New... » Ant Builder以添加Ant文件作为构建步骤。 Set Refresh to "project" so that generated files show up in the project. 将“刷新”设置为“项目”,以便生成的文件显示在项目中。 Be sure to put the build step after the Java Builder step because javah reads compiled class files. 确保将构建步骤放在Java Builder步骤之后,因为javah会读取已编译的class文件。

Now, you'll always have up-to-date header files whenever you change your Java code. 现在,无论何时更改Java代码,您将始终拥有最新的头文件。 You just have list the applicable classes in the Ant file. 您只需在Ant文件中列出适用的类。

<?xml version="1.0" encoding="UTF-8"?>
<project name="javah">
    <mkdir dir="javah" />

    <javah classpath="bin" destdir="javah">
        <!-- list classes here -->
        <class name="com.example.MyClass" />
    </javah>
</project>

You can get a lot more sophisticated in the Ant script but the above is sufficient. 您可以在Ant脚本中获得很多复杂的知识,但是以上内容就足够了。

I used to do it using command line. 我曾经使用命令行来做到这一点。

  • go to the source file directory. 转到源文件目录。

  • javac filename.java to generate filename.class file. javac filename.java生成filename.class文件。

  • javah filename to generate filename.h file. javah filename生成filename.h文件。

You may refer to javac and javah for more help. 您可以参考javacjavah以获得更多帮助。

What about this utility in the JDK? 在JDK中该实用程序如何? javah 爪哇

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

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