Sona
Sona

Reputation: 177

How to create jni header file with IntelliJ IDEA

I want to create the head file in IntelliJ IDEA. This is the way I do it:

file>setting>Tools>External Tools

then I click +, specify the title and set the parameters as follow:

Program: C:\Program Files\Java\jdk1.8.0_25\bin

Parameters: -d C:\Users\Administrator\Documents\Visual Studio 2013\Projects\JniExampleLibrary -jni Example.JniExample

Working Directory: C:\Program Files\Java\jdk1.8.0_25\bin

but after clicking ok, there is no JniExample.h file in the path defined above with -d

Would you please tell me what is the problem with it?

Thanks in advance

Upvotes: 16

Views: 12427

Answers (2)

Gunnar Bernstein
Gunnar Bernstein

Reputation: 6222

I am using Java 11 (OpenJDK) to compile, so I do not have javah available.

Since now the option to create header files is included in javac, I added it as an compiler option.

  1. File->Settings->Build, Execution, Deployment->Compiler->Java Compiler
  2. In "Additional command line parameters" add: -h YourOutDir

(IntelliJ Community 2019.2, other versions should be similar)

Example:

-h C:/Development/Java/YourApp/cpp

I did not have any luck with "-h $FileDir$" or other macro commands of IntelliJ.

Upvotes: 8

kastmaster
kastmaster

Reputation: 389

Here are the IntelliJ IDEA settings I use to generate the .h file: (This applies to IDEA version 12.1.6, probably similar in other versions)

  1. File->Settings->External Tools
  2. Click the + button for the "Edit Tool" dialog
  3. The following are the form name/value pairs I used:
    • Name: javah
    • Group: Java
    • Description: Java Native Interface C Header and Stub File Generator
    • Options: Check All
    • Show in: Check All
    • Tool Settings...
    • Program: $JDKPath$\bin\javah.exe
    • Parameters: -jni -v -d $FileDir$ $FileClass$
    • Working directory: $SourcepathEntry$
  4. Click OK, Click OK
  5. Navigate to your java class with the native method
  6. With the java class being shown in the editor, go to Tools->Java->javah
  7. Notice the .h file that was generated in the same directory as your class.

Upvotes: 34

Related Questions