Javah error while using it in JNI

╄→尐↘猪︶ㄣ 提交于 2019-12-17 09:09:09

问题


Command:

javah -jni JavaHowTo

Result:

error: cannot access JavaHowTo 
class file for JavaHowTo not found

javadoc: error - Class JavaHowTo not found.
Error: No classes were specified on the command line.  Try -help.

I have set the class path correctly, but still i am getting this javah error.

Any solution for this will be much helpful.


回答1:


Try

javah -jni com.example.JavaHowTo

where com.example is your package.

You also need to run javah from the directory containing com/example/JavaHowTo.class

e.g. if your structure is

/home/user/com/example/JavaHowTo.class

run javah from

/home/user



回答2:


The following worked for me (Win7):

javah -classpath bin/classes -jni -d jni com.my.javaclass

I run this from the app main directory.

The problem was in the sub-directory classes




回答3:


I successfully use javah every day from my build scripts with the following options:

javah -d <outputdir> -classpath <classpath> <fully_qualified_class>

where:

'outputdir' is the directory where to put the generated header file

'classpath' contains an absolute path to the directory containing your root package (as mentionned by Glen)

'fully_qualified_class' is the name of the class containing native methods without .class extension

-jni option is not required (set by default)

Anyway you should check your class file has been generated: quite surprised you get a javadoc error too...




回答4:


I encountered the same error and using http://www.inonit.com/cygwin/jni/helloWorld/header.html link I was able to successfully generate the .h file. I used the following command

C:\Program Files\Java\jdk1.6.0_21\bin>javah -d C:\Prachi\Android\Workspace_QP_re
structure\HelloWorld\bin\example\jni -classpath C:\Prachi\Android\Workspace_QP_r
estructure\HelloWorld\bin example.jni.HelloWorld

here example.jni is my package name and C:\Prachi\Android\Workspace_QP_re structure\ is where my eclipse workspace path.

Hope this helps you




回答5:


I had this problem when I add to -classpath my source directory. I thought classes are in *.java files ;)

Javah is working on byte code so you have to add directory with your binaries to classpath

I made some eclipse task:

"Location"

${env_var:JAVA_HOME}\bin\javah.exe

"Arguments"

-verbose 
-force
-classpath ;${project_loc}\bin
-d ${project_loc}\jni 
${java_type_name}

Select file and Run




回答6:


In my case the problem was pretty stupid.. I try: javah com.aaa.bbb.ClassName.class

without ".class" I generate the .h file correctly

javah com.aaa.bbb.ClassName




回答7:


I am using Eclipse.

Command syntax:

javah -d <outputdir> -classpath <classpath> <fully_qualified_class>

outputdir - The directory where the header file is generated.

classpath - Contains an absolute path to the directory containing the root package.

fully_qualified_class - The name of the class containing native methods WITHOUT .class extension

The following file is generated:

C:\Users\John\workspace\JNITest\bin\com\canessa\john\jnitest\com_canessa_john_jnitest_CallMyCLib.h

The contents of the generated header file:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_canessa_john_jnitest_CallMyCLib */

#ifndef _Included_com_canessa_john_jnitest_CallMyCLib
#define _Included_com_canessa_john_jnitest_CallMyCLib
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_canessa_john_jnitest_CallMyCLib
 * Method:    helloInC
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_canessa_john_jnitest_CallMyCLib_helloInC
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif



回答8:


I had similar issues and used the -verbose option. I had to add a ; to the front of the classpath option.

C:\Projects\HelloWorld\src>javah -verbose -classpath c:\projects\HellowWorld\build\classes helloworld.HelloWorld
error: cannot access helloworld.HelloWorld
class file for helloworld.HelloWorld not found
javadoc: error - Class helloworld.HelloWorld not found.
[ Search Path: c:\jdk\jre\lib\resources.jar;c:\jdk\jre\lib\rt.jar;c:\jdk\jre\lib    \sunrsasign.jar;c:\jdk\jre\lib\jsse.jar;c:\jdk\jre\lib\jce.jar;c:\jdk\jre\lib\ch    arsets.jar;c:\jdk\jre\classes\c:\projects\HellowWorld\build\classes ]
Error: No classes were specified on the command line.  Try -help.

C:\Projects\HelloWorld\src>javah -verbose -classpath ;c:\projects\HellowWorld\build\classes helloworld.HelloWorld
[ Search Path: c:\jdk\jre\lib\resources.jar;c:\jdk\jre\lib\rt.jar;c:\jdk\jre\lib\sunrsasign.jar;c:\jdk\jre\lib\jsse.jar;c:\jdk\jre\lib\jce.jar;c:\jdk\jre\lib\charsets.jar;c:\jdk\jre\classes\;c:\projects\HellowWorld\build\classes ]
[Creating file helloworld_HelloWorld.h]
[search path for source files: .,c:\projects\HellowWorld\build\classes]
[search path for class files: c:\jdk\jre\lib\resources.jar,c:\jdk\jre\lib\rt.jar,c:\jdk\jre\lib\sunrsasign.jar,c:\jdk\jre\lib\jsse.jar,c:\jdk\jre\lib\jce.jar,c:\jdk\jre\lib\charsets.jar,c:\jdk\jre\classes,c:\jdk\jre\lib\ext\dnsns.jar,c:\jdk\jre\lib\ext\localedata.jar,c:\jdk\jre\lib\ext\sunjce_provider.jar,c:\jdk\jre\lib\ext\sunmscapi.jar,c:\jdk\jre\lib\ext\sunpkcs11.jar,.,c:\projects\HellowWorld\build\classes]
[loading .\helloworld\HelloWorld.class]
[loading java\lang\Object.class(java\lang:Object.class)]
[loading java\lang\Throwable.class(java\lang:Throwable.class)]
[loading java\lang\Class.class(java\lang:Class.class)]
[done in 409 ms]

Without the ;, this is the end of the search path: c:\jdk\jre\classes\c:\projects\HellowWorld\build\classes

I was able to get this far because of the examples above.




回答9:


For Windows...

FILE DETAILS

javaFileName: HelloWorld.java
packageCode: "package com.accessibility;"
importCode: "import com.accessibility.HelloWorld;"
javaFileLocation: "S:\Accessibility\tools\src\test\java\com\accessibility\HelloWorld.java"

The javaFileLocation should be viewed as having two parts...

classpath: "S:\Accessibility\tools\src\test\java"
relativeFilePath: "com\accessibility\HelloWorld.java"

CREATE CLASS FILE

The second line below was run from the Windows Command Prompt.

{javacLocation} -d {ouputDirectory} {javaFileLocation}
"C:\Program Files\Java\jdk1.6.0_26\bin\javac.exe" -d "S:\Accessibility\tools\src\test\java" "S:\Accessibility\tools\src\test\java\com\accessibility\HelloWorld.java"

This created the HelloWorld.class file

classFileName: HelloWorld.class 
classFileLocation: "S:\Accessibility\tools\src\test\java\com\accessibility\HelloWorld.class"

CREATE HEADER FILE

The second line below was run from the Windows Command Prompt.

{javahLocation} -o {outputFile} -classpath {classpath} {importName}
"C:\Program Files\Java\jdk1.6.0_26\bin\javah.exe" -o "S:\Accessibility\tools\src\test\java\com\accessibility\HelloWorld.h" -classpath "S:\Accessibility\tools\src\test\java" com.accessibility.HelloWorld

This created the HelloWorld.h file

classFileName: HelloWorld.h
classFileLocation: "S:\Accessibility\tools\src\test\java\com\accessibility\HelloWorld.h"



回答10:


issue on Cygwin:

javah does not work properly from Cygwin terminal:

$ cd /cygdrive/c/Workspace/prj/bin
$ ls com/example/JavaHotTo.class
com/example/JavaHotTo.class
$ javah com.example.JavaHowTo
Error: Could not find class file for 'com.example.JavaHowTo'.

But it works perfectly using the option -classpath .

$ javah -classpath . com.example.JavaHowTo
$ ls *.h
com_example_JavaHotTo.h

More tricks:

  • The option -jni is by default, therefore it is not require.
  • You can give relative/absolute classpath
  • But javah on MS-Windows prefers path à la C:\Workspace\prj\bin
  • Use cygpath -w /cygdrive/c/Workspace/prj/bin to convert into MS-Windows format
  • Use option -d your/jni/headers/path to write headers in another directory
  • javah creates the path your/jni/headers/path if it does not exist (see below)

    $ javah -d jni/headers -classpath . com.example.JavaHowTo
    $ ls       jni\headers
    com_example_JavaHotTo.h
    $ javah -d jni\path -classpath . com.example.JavaHowTo
    $ ls       jni\path
    com_example_JavaHotTo.h
    



回答11:


It worked with me:
For window:
If path file .class: D:\workspace\JNI Demo\build\classes\jni\demo and in Netbeans hava package: jni.demo.HelloWorld.java
First, I buijd file .java into file .class.
Path file .class: D:\workspace\JNI Demo\build\classes\jni\demo\HelloWorld.class
Second, I dir D:\workspace\JNI Demo\build\classes
And javah -jni jni.demo.HelloWorld
Finally, File result .j created in path: D:\workspace\JNI Demo\build\classes




回答12:


Go to your "bin" directory of your Java project through the cmd Windows command line.

cd "Path to the bin directory of your java project"

Then,

javah "Your Package"."Name Of Your Java Class"



回答13:


Following worked for me... Go to project path from terminal like,

/home/usr/Desktop/Workspace/ProjectName/src/main/java

and hit following command,

javah -jni com.abc.test.callfileName (Without .class extension)

com_abc_test_className.h file will get created in following path /home/usr/Desktop/Workspace/ProjectName/src/main/java

finally the command looks like, [my-machine/usr] $ /home/usr/Desktop/Workspace/ProjectName/src/main/java/src/main/java>javah -jni com.abc.test.className




回答14:


How to generate a `JNI Header` file from an `Android class` file using `javah` (`.java` or `.class` BOTH work equally well).
For Eclipse project on Windows:
Goto the directory where the src, bin and jni folders are (so we can use relative paths to src or bin and jni): cd C:\Android\workspace5\AndroidImageFilter I assume `javah` is in your `PATH` (It is in something like : `C:\Program Files\Java\jdk1.7.0_79\bin\javah.exe`): javah -classpath c:\Android\sdk\platforms\android-19\android.jar;./src/ -d ./jni cn.Ragnarok.NativeFilterFunc ^ ^ ^ ^ ^ ^ ^ ^ ^ | | | | | | | | | Your android:targetSdkVersion="16"----------------------------+ | | | | | | | | | | | | | | | | | | | | | | | path to android.jar ---------+---------------------------------------+ | | | | | | | | | | | | | | | | | | | | | | | | semicolon (;) important ! --------------------------------------------------+ | | | | | | | | | | You can use the source directory (yes you can !) (OR the /bin/classes/ dir)-----+ | | | | | | | | Where to put the output file (cn_Ragnarok_NativeFilterFunc.h in this case)-----------+---+ | | | | The dot (.) spearated full package name path to the .java source file WITH file name --------+ | | File name of a java class -------------------------------------------------------------------------------------+ For Android Studio on Windows e'g': ^ cd C:\Android\workspace5\AndroidImageFilter\app\src\main | and usr java INSTEAD of src ----------------------------------------------------+

See http://kn-gloryo.github.io/Build_NDK_AndroidStudio_detail/ for a better way.

If you have a lot of filenames (of java classes) you can use a file with a list of classes.
If you are on Linux or MAC-OS, use ":" (colon) to separate the directories for classpath rather than ";"
See reference http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/javah.html



来源:https://stackoverflow.com/questions/986262/javah-error-while-using-it-in-jni

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!