Javah error while using it in JNI

Viewed 53383

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.

14 Answers

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

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

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

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

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.

Related