1. 程式人生 > >Android問題集錦轉載之三:Javah 常見錯誤記錄-NDK與JNI除錯

Android問題集錦轉載之三:Javah 常見錯誤記錄-NDK與JNI除錯

測試檔案:hello-jni/src/com/example/hellojni/HelloJni.java

/**
 * 該檔案來自 Android NDK Sample - HelloJni, 為了便於說明問題,我作了一些修改。
 */
package com.example.hellojni;

public class HelloJni
{
    public native String  stringFromJNI();

    public native String  unimplementedStringFromJNI();

    static {
        System.loadLibrary("hello-jni");
    }
}

錯誤一

[email protected]:hellojni$ javac HelloJni.java 
[email protected]:hellojni$ ls
Hello.class  Hello.h  Hello.java  HelloJni.class  HelloJni.java
[email protected]:hellojni$ javah -jni HelloJni
error: cannot access HelloJni
bad class file: ./HelloJni.class
class file contains wrong class: com.example.hellojni.HelloJni
Please remove or make sure it appears in the correct subdirectory of the classpath.
com.sun.tools.javac.util.Abort
	at com.sun.tools.javac.comp.Check.completionError(Check.java:164)
	at com.sun.tools.javadoc.DocEnv.loadClass(DocEnv.java:149)
	at com.sun.tools.javadoc.RootDocImpl.<init>(RootDocImpl.java:77)
	at com.sun.tools.javadoc.JavadocTool.getRootDocImpl(JavadocTool.java:159)
	at com.sun.tools.javadoc.Start.parseAndExecute(Start.java:330)
	at com.sun.tools.javadoc.Start.begin(Start.java:128)
	at com.sun.tools.javadoc.Main.execute(Main.java:66)
	at com.sun.tools.javah.Main.main(Main.java:147)
javadoc: error - fatal error
2 errors

錯誤原因,沒有在正確的路徑下執行 javah 命令,應該在原始碼根目錄下執行。

錯誤二:

[email protected]:hellojni$ cd ../../../
[email protected]:src$ ls
com
[email protected]:src$ javah -jni HelloJni
error: cannot access HelloJni
class file for HelloJni not found
javadoc: error - Class HelloJni not found.
Error: No classes were specified on the command line.  Try -help.

錯誤原因:Classes 引數要使用完整類名,也就是說要加上包名

錯誤四:

[email protected]:src$ javah -jni com/example/hellojni/HelloJni
javadoc: error - Illegal package name: "com/example/hellojni/HelloJni"
1 error

錯誤原因:完整類名格式錯誤

[email protected]:src$ javah -jni com.example.hellojni.HelloJni

OK,編譯通過。

如果還有錯誤,說是類找不到還是什麼的,請嘗試新增 -classpath . 引數。如下:

[email protected]:src$ javah -jni -classpath . com.example.hellojni.HelloJni