user2381422
user2381422

Reputation: 5805

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?

I have Java project, which communicates with C++ code through JNI. The challenge is now I need to add new methods. I first started by declaring the native methods in the java code. Now I need to regenerate the header file for the JNI methods. I work in Eclipse, not sure how to do this.

Upvotes: 2

Views: 9759

Answers (3)

Tom Blodget
Tom Blodget

Reputation: 20812

This is not built into Eclipse but it is easy to do:

  1. Go File » New... » XML File to create an Ant file (code below) in your project.
  2. Go Project » Properties » Builders » New... » Ant Builder to add the Ant file as a build step. 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.

Now, you'll always have up-to-date header files whenever you change your Java code. You just have list the applicable classes in the Ant file.

<?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.

Upvotes: 3

Annie Kim
Annie Kim

Reputation: 905

I used to do it using command line.

  • go to the source file directory.

  • javac filename.java to generate filename.class file.

  • javah filename to generate filename.h file.

You may refer to javac and javah for more help.

Upvotes: 3

user2496748
user2496748

Reputation: 65

What about this utility in the JDK? javah

Upvotes: 0

Related Questions