通过javah可以自动生成相应的JNI的.h文件,运行“javah -help” 命令显示用法如下:

 
  
  1. Z:\>javah -help 
  2. Usage: javah [options] <classes> 
  3.  
  4. where [options] include: 
  5.  
  6.         -help                 Print this help message and exit 
  7.         -classpath <path>     Path from which to load classes 
  8.         -bootclasspath <path> Path from which to load bootstrap classes 
  9.         -d <dir>              Output directory 
  10.         -o <file>             Output file (only one of -d or -o may be used) 
  11.         -jni                  Generate JNI-style header file (default
  12.         -version              Print version information 
  13.         -verbose              Enable verbose output 
  14.         -force                Always write output files 
  15.  
  16. <classes> are specified with their fully qualified names (for instance, java.lang.Object). 

 

example:
 
hello-jni应用程序所在目录为 F:\hello-jni, 编译生成的class文件在bin\classes文件夹下,那么调用javah时的classpath 必须指向该路径
 
第一种情况:
F:\hello-jni>javah -classpath   bin/classes  com.example.hellojni.HelloJni 
在应用程序根目录下,那么classpath 的相对路径为 bin/classes
 
第二种情况:
F:\hello-jni\bin>javah -classpath   classes  com.example.hellojni.HelloJni
在bin目录下,那么classpath的相对路径为 classes
 
第三种情况:
F:\hello-jni\bin\classes>javah -classpath   .  com.example.hellojni.HelloJni
在classes目录下,那么classpath应该为当前目录,即 .
 
第四种情况:
F:\temp>javah -classpath   F:\hello-jni\bin\classes   com.example.hellojni.HelloJni
此时classpath 应该指向classes所在的绝对路径,即F:\hello-jni\bin\classes