Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating Java JNI Header

All I'm trying to do is generate a JNI Header file using JDK Javah.exe program, but no matter what I try I keep getting the error message "Error: could not find class file for 'ddg.ndkTest.NativeLib'" (where ddg.ndkTest is the namespace and NativeLib is the java class file). Also, please note that to simplify the process further, I've copied my entire Android project to the Java directory.

I've tried the following commands, none of which have worked:

  • javah -jni ddg.ndkTest.NativeLib
  • javah -verbose -jni -classpath \NDKTest\bin\classes ddg.ndkTest.NativeLib
  • C:\Program Files\Java\jdk1.7.0_02\bin>javah.exe -jni -classpath \NDKTest\bin\classes\ ddg.ndkTest.NativeLib

If my package is called; "ddg.ndkTest", and my class is; "NativeLib.java", and my classpath above is correct. What should I write in my command window?

like image 595
Ice Phoenix Avatar asked Jul 03 '12 14:07

Ice Phoenix


1 Answers

Update for java >= Java9

Since Java9 the command javah has been flagged with a deprecation warning and starting from Java10 it has been deleted from the JDK since its functionalities are now included in the javac command.

So in order to do the same from Java9 you should run: javac -h . NativeLib.java

Note I am assuming you are in the correct directory

The main difference besides the command is that now the JNI header creation is targeting the source code in the .java file and not the compiled .class

-h directory Specifies where to place generated native header files.

When you specify this option, a native header file is generated for each class that contains native methods or that has one or more constants annotated with the java.lang.annotation.Native annotation. If the class is part of a package, then the compiler puts the native header file in a subdirectory that reflects the package name and creates directories as needed.

https://docs.oracle.com/en/java/javase/11/tools/javac.html#GUID-AEEC9F07-CB49-4E96-8BC7-BCC2C7F725C9

like image 124
rakwaht Avatar answered Sep 30 '22 15:09

rakwaht