6

I just have an annoying problem with jni when i compile the native method in java class with javah to generate JNI header files.

If the class has used 3rd-party package, For example: org.opencv.core.Mat, then the javah will show the error that can't find the org.opencv.core.Mat class.

The OpenCV sample code as below:

package org.opencv.samples.fd;

import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;

public class DetectionBasedTracker
{
public DetectionBasedTracker(String cascadeName, int minFaceSize) {
    mNativeObj = nativeCreateObject(cascadeName, minFaceSize);
}

public void start() {
    nativeStart(mNativeObj);
}

public void stop() {
    nativeStop(mNativeObj);
}

public void setMinFaceSize(int size) {
    nativeSetFaceSize(mNativeObj, size);
}

public void detect(Mat imageGray, MatOfRect faces) {
    nativeDetect(mNativeObj, imageGray.getNativeObjAddr(), faces.getNativeObjAddr());
}

public void release() {
    nativeDestroyObject(mNativeObj);
    mNativeObj = 0;
}

private long mNativeObj = 0;

private static native long nativeCreateObject(String cascadeName, int minFaceSize);
private static native void nativeDestroyObject(long thiz);
private static native void nativeStart(long thiz);
private static native void nativeStop(long thiz);
private static native void nativeSetFaceSize(long thiz, int size);
private static native void nativeDetect(long thiz, long inputImage, long faces);
}

First, I used the command

javah -classpath bin/classes -bootclasspath (the directory of android.jar) -d jni (packageName + ClassName) , shows the error "can't find the org.opencv.core.Mat

Then I modified the command to

javah - classpath bin/classes - bootclasspath (the dir of android.jar) ; (the dir of the opencv lib jar)  -d jni ..." ", this time it shows error

Exception

Exception in thread "main" java.lang.IllegalArgumentException: Not a valid class
 name: E:\Computer_Language\Java\soft_android\OpenCV-2.4.3-rc-android-sdk\OpenCV
-2.4.3-rc-android-sdk\sdk\java\bin\opencv library - 2.4.3.jar
    at com.sun.tools.javac.api.JavacTool.getTask(JavacTool.java:177)
    at com.sun.tools.javac.api.JavacTool.getTask(JavacTool.java:68)
    at com.sun.tools.javah.JavahTask.run(JavahTask.java:509)
    at com.sun.tools.javah.JavahTask.run(JavahTask.java:335)
    at com.sun.tools.javah.Main.main(Main.java:46)

I think, adding the directory of opencv lib in -bootclasspath is useful and neccessary. The error is because i just added two path in -bootclasspath or the format is something wrong?

Really confused. Please give some help , thank u!

Lokesh Mehra
  • 545
  • 6
  • 16
santi
  • 117
  • 3
  • 11
  • Would that be a problem to create bare bones `NativeWrapper` class without any external imports and move all native methods there? You already have them `static native`, so it should be matter of just prepending the existing native calls with `NativeWrapper.` – Pavel Zdenek Nov 18 '12 at 12:37
  • @PavelZdenek NativeWrapper ? I just try to use javah to generate the C Header File of native method in android code. I have tried the situation when the android class contains native method without use opencv, and it works. When the class contains the opencv lib, javah shows error as the question mentioned.Is there a easy way to generate the .h File? how could i write the bootclasspath everytime ? thanks. – santi Nov 19 '12 at 04:37
  • *"I have tried the situation when the android class contains native method without use opencv, and it works."* So why don't you do it that way? – Pavel Zdenek Nov 19 '12 at 09:07
  • @PavelZdenek you are right, i tried this before, it works. But the problem is that when use the opencv class in the java class contains native method just as the sample above shows. It gets error ! – santi Nov 19 '12 at 14:32
  • plz use ";" instead ":" in above answer in windows : javah -classpath /home/zijun/Dev/adt/OpencvAndroid/sdk/java/bin/classes;/home/zijun/workspace/LocTM/bin/classes -jni com.brainport.loctm.TMatching – Saber Solooki Sep 02 '13 at 08:26

2 Answers2

6

This is what I did:

1.Open Command line, type to the (project)/bin/classes: 2.type: javah -classpath (opencv4android sdk path)/java/bin/classes:(your project location)/bin/classes -jni (your java class file that contains the native library interfaces)

In my project. I did:

javah -classpath /home/zijun/Dev/adt/OpencvAndroid/sdk/java/bin/classes:/home/zijun/workspace/LocTM/bin/classes -jni com.brainport.loctm.TMatching

That works on Linux Ubuntu 12.04.02 64bit OS

ZijunLost
  • 1,584
  • 2
  • 20
  • 25
1

I encountered the same problem too,it cost me half day.I just copy DetectionBasedTracker.java to my poject.I use Android Studio.When I use ExternalTools>javah to generate .h files,the console print cant find org.opencv.core.Mat.copying mat.java & matofRect.java to the directory DetectionBasedTracker.java exists just cause more java class cant be find.finally,I find this problem was caused by the import * clause in java file.and we seldom use java class in native method we defined.so I cut these java method and got this:

package cn.ntu.tonguefur.nativemethod;
public class DetectionBasedTracker{
private static native long nativeCreateObject(String cascadeName,int minFaceSize);
//Other native methods
private static native void nativeDetect(long thiz,long inputImage,long faces);
}

Now you can freely use javah command to generate .h file.After that,add java methods and import packages you need.

何德福
  • 81
  • 3