3

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.

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:

$ 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/'

Thanks for any help.

Community
  • 1
  • 1
user3019612
  • 259
  • 2
  • 7
  • 15
  • possible duplicate of [Android, generate jni Header Files with javah , show error that can't find org.opencv.core.Mat](http://stackoverflow.com/questions/13438368/android-generate-jni-header-files-with-javah-show-error-that-cant-find-org-o) – Alex Cohn Feb 09 '14 at 19:48
  • I tried following the advice from that thread, but got errors. I should've mentioned it my original post, but I've now edited it. – user3019612 Feb 09 '14 at 20:43
  • have you tried `javah -classpath c:\Users\Majid\Documents\OpenCV4Android\OpenCVLib2.4.8\bin\classes\org\opencv;c:\Users\Majid\Documents\OpenCV4Android\iCam\bin\classes\com\example\icam -jni -d jni/ com.example.icam.nativeRDE`? Or maybe (I don't have the environment ready to verify), you can use forward (Unix-style) slash **/**. The reason is that **javah** is not a **cygwin** executable, therefore it does not understand `/cygdrive`. You already noticed that you need **";"** separator in Windows, not **":"** as in the [referenced topic](http://stackoverflow.com/questions/13438368) – Alex Cohn Feb 10 '14 at 07:54
  • Yes, but it still was not working. I think I have a workaround, so hopefully should be fine. – user3019612 Feb 12 '14 at 14:13
  • generally speaking, you don't need `javah` to build and run your JNI code on Android. – Alex Cohn Feb 12 '14 at 14:56
  • On the 2nd use of javah, I would expect cygwin to parse the line as two commands, separated by that semicolon. That would explain why bash complained about finding a directory -- it wanted an executable after the semicolon. – donjuedo May 15 '19 at 18:22

3 Answers3

5

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

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

Javah -jni -classpath C:\ProjectName\src com.abc.YourClassName

ylmzekrm1223
  • 103
  • 5
  • 15
5

Solution: Generating header file with JNI using 'javah'

         ***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

JDK Location is

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

JNI Folder Location is

    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

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

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 this com_example_mpankaj_jnip_JniExample.h

Pankaj Malviya
  • 109
  • 1
  • 3
  • I meet same problem, help me plz [http://stackoverflow.com/questions/39261963/error-class-org-opencv-core-mat-could-not-be-found](http://stackoverflow.com/questions/39261963/error-class-org-opencv-core-mat-could-not-be-found) – Dennis Lu Sep 01 '16 at 02:58
1

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

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

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.)
kunal
  • 187
  • 1
  • 2
  • 12