Javah error while using it in JNI

前端 未结 14 1673
离开以前
离开以前 2020-12-01 05:27

Command:

javah -jni JavaHowTo

Result:

error: cannot access JavaHowTo 
class file for JavaHowTo not found

javadoc: error -          


        
相关标签:
14条回答
  • 2020-12-01 05:46

    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

    0 讨论(0)
  • 2020-12-01 05:48

    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
      
    0 讨论(0)
  • 2020-12-01 05:51

    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

    0 讨论(0)
  • 2020-12-01 05:51

    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

    0 讨论(0)
  • 2020-12-01 05:52

    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"
    
    0 讨论(0)
  • 2020-12-01 05:53

    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

    0 讨论(0)
提交回复
热议问题