44

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.

madth3
  • 7,275
  • 12
  • 50
  • 74
krishnakumar
  • 2,187
  • 5
  • 21
  • 24

14 Answers14

82

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
Jason D
  • 8,023
  • 10
  • 33
  • 39
Glen
  • 21,816
  • 3
  • 61
  • 76
  • 1
    Where should the h file reside if thinking about good practice? – basickarl Nov 24 '14 at 11:00
  • This works for me. I have tried with -classpath and providing the path for my class and it says no classess specified. Just Run javah -jni it works – shailesh Jun 17 '15 at 17:45
13

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

jankos
  • 896
  • 1
  • 11
  • 16
11

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

dilig0
  • 1,006
  • 2
  • 10
  • 18
  • @dilig0 I get that javadoc error too if the .class file can't be found. Maybe javah uses javadoc to add the comments to the .h file – Glen Jun 12 '09 at 13:32
  • The Error is solved. i changed the environmental variables and now it works fine. thanks friends – krishnakumar Jun 15 '09 at 05:45
  • I have a similar problem - I have a class that's below the class containing the native functions in terms of package hierarchy, and javah can't find that class, even though the package names go through fine in Eclipse. Any suggestions so as how to manipulate classpath to deal with that scope problem? – sparkFinder Jul 27 '11 at 23:36
  • 1
    @krishnakumar, what were the environment variables set to originally, and what did you change them to? – Alan Oct 27 '16 at 14:41
9

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

Andrei Sfat
  • 8,440
  • 5
  • 49
  • 69
  • For me I had to do this from the eclipse project directory: 'javah -d jni -classpath bin/classes example.jni.HelloWorld' – Chris May 02 '12 at 19:27
4

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

Konrad Nowicki
  • 1,929
  • 1
  • 14
  • 11
3

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
Barry Michael Doyle
  • 9,333
  • 30
  • 83
  • 143
  • I made it work by pointing to source folder directory via -classpath and then giving the package and java file name (that contains the native keyword.) The point here is javah needs to be able to use the files as if jdk compiles them. classpath keyword should point to source file folder, not to bin folder...And then you should give the package path to class that contains the native keyword. Going to folder of src and running javah and pacageName and ClassFileName doesnot work. (This maybe because of need to fetch all java files. This worked for me...) – ilke Muhtaroglu Jan 28 '19 at 13:18
3

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

2

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"
Scott Izu
  • 2,229
  • 25
  • 12
2

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.

Jon
  • 198
  • 1
  • 2
  • 12
1

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
    
oHo
  • 51,447
  • 27
  • 165
  • 200
1

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

Hoang Thai
  • 291
  • 3
  • 10
0

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"
Max Dy
  • 307
  • 3
  • 7
0

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

Sarang.C
  • 61
  • 1
  • 7
0
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

Jon Goodwin
  • 9,053
  • 5
  • 35
  • 54