使用'javah'用JNI生成头文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21663423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:55:44  来源:igfitidea点击:

Generating header file with JNI using 'javah'

javaandroidopencvjava-native-interfacejavah

提问by user3019612

I'm trying to use JNI for an Android application using the OpenCV4Android library. I can generate a header file without using the opencv library, but I get an error whenever the class imports anything. I assume it needs to link to the library, but I'm not sure how to do that? I'm using cygwin on a Windows 8.1 64 bit machine.

我正在尝试将 JNI 用于使用 OpenCV4Android 库的 Android 应用程序。我可以在不使用 opencv 库的情况下生成头文件,但是每当类导入任何内容时我都会收到错误消息。我认为它需要链接到图书馆,但我不知道该怎么做?我在 Windows 8.1 64 位机器上使用 cygwin。

original output:

原始输出:

$ javah -jni -classpath ./bin/classes -d jni/ com.example.icam.nativeRDE  
Error: Class org.opencv.core.Mat could not be found.

After following advice from: Android, generate jni Header Files with javah , show error that can't find org.opencv.core.Mat, I get the following output:

根据以下建议:Android, generate jni Header Files with javah , show error that can't find org.opencv.core.Mat,我得到以下输出:

$ javah -classpath /cygdrive/c/Users/Majid/Documents/OpenCV4Android/OpenCVLib2.4.8/bin/classes/org/opencv/;/cygdrive/c/Users/Majid/Documents/OpenCV4Android/iCam/bin/classes/com/example/icam/ -jni -d jni/ com.example.icam.nativeRDE
Error: no classes specified
-bash: /cygdrive/c/Users/Majid/Documents/OpenCV4Android/iCam/bin/classes/com/example/icam/: is a directory

I've tried:

我试过了:

  • removing '/' after icam
  • adding nativeRDE after 'icam/'
  • adding nativeRDE.class after 'icam/'
  • 在 icam 后删除“/”
  • 在“icam/”之后添加 nativeRDE
  • 在“icam/”之后添加 nativeRDE.class

Thanks for any help.

谢谢你的帮助。

回答by ylmzekrm1223

Javah takes a fully qualified class name, and classpath. Class name must be with full package name.
Ex:fullPackageName.className

Javah 采用完全限定的类名和类路径。类名必须是完整的包名。
前任:fullPackageName.className

Class path is your src folder not bin folder Your classpath must be c\Users\Majid\Documents\OpenCV4Android\iCam\src

类路径是您的 src 文件夹而不是 bin 文件夹您的类路径必须是 c\Users\Majid\Documents\OpenCV4Android\iCam\src

Javah -jni -classpathC:\ProjectName\srccom.abc.YourClassName

Javah -jni -classpathC:\ProjectName\srccom.abc.YourClassName

回答by Pankaj Malviya

Solution: Generating header file with JNI using 'javah'

解决方案:使用'javah'生成带有JNI的头文件

         ***I am using Window 10 and Android Studio 2.1.2.***

Suppose APP (JNIP my app Name) location is

      E:\test\JNIP and you wrote native methods in JniExample.java file

假设 APP(JNIP 我的应用程序名称)位置是

      E:\test\JNIP and you wrote native methods in JniExample.java file

JDK Location is

JDK 位置是

   C:\Program Files\Java\jdk1.8.0_51\bin> 

JNI Folder Location is

JNI 文件夹位置是

    E:\test\JNIP\app\src\main\JNI (where, you want to create header file)

Class Location is

班级位置是

 E:\test\JNIP\app\build\intermediates\classes\debug\com\example\mpankaj\jnip\JniExample.java

Android.jar location is

Android.jar 位置是

C:/Users/mpankaj/AppData/Local/Android/Sdk/platforms/android-23/android.jar

First Build your project before running below command

在运行以下命令之前首先构建您的项目

Write Command on coommand prompt/Terminal for .h file creation

在命令提示符/终端上写入命令以创建 .h 文件

javah -d (JNI Folder Location) -classpath (JAR Locaion);(class Location)

Example Command using above details for Command

示例命令使用上述命令的详细信息

   C:/Program Files/Java/jdk1.8.0_51/bin>javah -d E:/test/JNIP/app/src/main/JNI -classpath C:/Users/mpankaj/AppData/Local/Android/Sdk/platforms/android-23/android.jar;E:\test\JNIP\app\build\intermediates\classes\debug com.example.mpankaj.jnip.JniExample 

After this you will get .h file like thiscom_example_mpankaj_jnip_JniExample.h

在此之后,您将获得像这样的 .h 文件com_example_mpankaj_jnip_JniExample.h

回答by kunal

You can even execute javah from eclipse with much simplicity. I tried these below steps and they are working I referred the below link for solution http://www.lithiumhead.com/notes/windows_jni

您甚至可以非常简单地从 eclipse 执行 javah。我尝试了以下步骤并且它们正在工作我参考了以下解决方案的链接http://www.lithiumhead.com/notes/windows_jni

Step by Step Guide

分步指南

  1. Start Eclipse. Preferably create a new workspace called WorkSpaceEclipseJNI
  2. From the menu select File>New>Java Project
  3. Enter Project Name as 01Java_HelloWorld
  4. Click Next >
  5. Click Finish
  6. In the Package Explorer expand 01Java_HelloWorld
  7. Right click src folder and select New>Package
  8. Enter Name as com.lithiumhead.jni
  9. Click Finish
  10. In the Package Explorer under 01Java_HelloWorld > src right click com.lithiumhead.jni and select New>Class
  11. Enter Name as HelloWorld
  12. Click Finish
  13. Paste the following code into
  1. 启动日食。最好创建一个名为 WorkSpaceEclipseJNI 的新工作区
  2. 从菜单中选择 File>New>Java Project
  3. 输入项目名称为 01Java_HelloWorld
  4. 单击下一步 >
  5. 单击完成
  6. 在 Package Explorer 中展开 01Java_HelloWorld
  7. 右键单击 src 文件夹并选择 New>Package
  8. 输入名称为 com.lithiumhead.jni
  9. 单击完成
  10. 在 Package Explorer 下 01Java_HelloWorld > src 右键单击​​ com.lithiumhead.jni 并选择 New>Class
  11. 输入名称为 HelloWorld
  12. 单击完成
  13. 将以下代码粘贴到

HelloWorld.java

你好世界

package com.lithiumhead.jni;

class HelloWorld {
 public native void sayHello();

 static {
  System.loadLibrary("HelloWorld");
}

 public static void main(String[] args) {
  HelloWorld h = new HelloWorld();
  h.sayHello();
}

}

}

  1. From the menu select Run>External Tools>External Tools Configurations…
  2. Highlight Program in the list in the left pane
  3. Press the New button
  4. Enter Name as javah - C Header and Stub File Generator
  5. For the Location browse to locate javah.exe in the JDK installation folder (will be something like C:\Program Files\Java\jdk1.7.0\bin\javah.exe)
  6. Enter Working Directory as: ${project_loc}/bin/
  7. Enter Arguments as -jni ${java_type_name}
  8. Click Apply
  9. Switch to the Common tab
  10. Select the checkbox next to External Tools under Display in favourites menu
  11. Click Apply
  12. Click Close
  13. Deselect Build Automatically from Project Menu
  14. In the Package Explorer right click 01Java_HelloWorld and select Build Project
  15. In the Package Explorer highlight HelloWorld.java
  16. From the menu select Run>External Tools>1 javah - C Header and Stub File Generator (This will generate the header file for the C code com_lithiumhead_jni_HelloWorld.h placed in the bin folder of 01Java_HelloWorld Java Project.)
  1. 从菜单中选择运行>外部工具>外部工具配置...
  2. 在左窗格的列表中突出显示程序
  3. 按新建按钮
  4. 输入名称为 javah - C 头文件和存根文件生成器
  5. 对于 Location 浏览以在 JDK 安装文件夹中找到 javah.exe(类似于 C:\Program Files\Java\jdk1.7.0\bin\javah.exe)
  6. 输入工作目录为:${project_loc}/bin/
  7. 输入参数为 -jni ${java_type_name}
  8. 点击应用
  9. 切换到常用选项卡
  10. 选中收藏夹菜单中显示下的外部工具旁边的复选框
  11. 点击应用
  12. 单击关闭
  13. 从项目菜单中取消选择自动构建
  14. 在 Package Explorer 中右键单击 01Java_HelloWorld 并选择 Build Project
  15. 在 Package Explorer 中突出显示 HelloWorld.java
  16. 从菜单中选择 Run>External Tools>1 javah - C Header and Stub File Generator(这将生成 C 代码的头文件 com_lithiumhead_jni_HelloWorld.h 放在 01Java_HelloWorld Java 项目的 bin 文件夹中。)