在 JNI 中使用它时出现 Javah 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/986262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:54:09  来源:igfitidea点击:

Javah error while using it in JNI

javajava-native-interfaceclasspath

提问by krishnakumar

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

我已经正确设置了类路径,但仍然出现此javah错误。

Any solution for this will be much helpful.

对此的任何解决方案都会很有帮助。

回答by Glen

Try

尝试

javah -jni com.example.JavaHowTo

where com.exampleis your package.

com.example你的包裹在哪里。

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

您还需要从包含的目录中运行 javah com/example/JavaHowTo.class

e.g. if your structure is

例如,如果您的结构是

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

run javah from

运行 javah

/home/user

回答by dilig0

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

我每天都从我的构建脚本中成功使用 javah,并具有以下选项:

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

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

where:

在哪里:

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

'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)

'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

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

-jni option is not required (set by default)

-jni 选项不是必需的(默认设置)

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

无论如何,你应该检查你的类文件是否已经生成:很惊讶你也得到一个 javadoc 错误......

回答by Konrad Nowicki

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

当我将目录添加到 -classpath 时,我遇到了这个问题。我认为类在 *.java 文件中;)

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

Javah 正在处理字节码,因此您必须将包含二进制文件的目录添加到类路径

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

选择文件并运行

回答by Prachi Shrivastava

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

我遇到了同样的错误,使用http://www.inonit.com/cygwin/jni/helloWorld/header.html链接我能够成功生成 .h 文件。我使用了以下命令

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.

这里 example.jni 是我的包名,C:\Prachi\Android\Workspace_QP_re structure\是我的 eclipse 工作区路径。

Hope this helps you

希望这对你有帮助

回答by Marco McRoni

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

在我的情况下,问题非常愚蠢..我尝试:javah com.aaa.bbb.ClassName。班级

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

没有“.class”我正确生成了.h文件

javah com.aaa.bbb.ClassName

javah com.aaa.bbb.ClassName

回答by jankos

The following worked for me (Win7):

以下对我有用(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

问题出在子目录类中

回答by Jon

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

我有类似的问题并使用了 -verbose 选项。我不得不添加一个; 到类路径选项的前面。

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

没有;,这是搜索路径的结尾:c:\jdk\jre\classes\c:\projects\HellowWorld\build\classes

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

由于上面的例子,我能够做到这一点。

回答by olibre

issue on Cygwin:

Cygwin 上的问题:

javahdoes not work properly from Cygwin terminal:

javah从 Cygwin 终端无法正常工作:

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

但它可以完美地使用该选项 -classpath .

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

More tricks:

更多技巧:

  • The option -jniis by default, therefore it is not require.
  • You can give relative/absolute classpath
  • But javahon MS-Windows prefers path à laC:\Workspace\prj\bin
  • Use cygpath -w /cygdrive/c/Workspace/prj/binto convert into MS-Windows format
  • Use option -d your/jni/headers/pathto write headers in another directory
  • javahcreates the path your/jni/headers/pathif 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
    
  • 该选项-jni是默认的,因此它不是必需的。
  • 你可以给相对/绝对 classpath
  • 但是javah在 MS-Windows 上更喜欢路径à laC:\Workspace\prj\bin
  • 使用cygpath -w /cygdrive/c/Workspace/prj/bin转换成MS-Windows格式
  • 使用选项-d your/jni/headers/path在另一个目录中写入标题
  • javahyour/jni/headers/path如果路径不存在则创建路径(见下文)

    $ 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
    

回答by Scott Izu

For Windows...

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

javaFileLocation 应该被视为有两部分......

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.

下面的第二行是从 Windows 命令提示符运行的。

{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

这创建了 HelloWorld.class 文件

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.

下面的第二行是从 Windows 命令提示符运行的。

{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

这创建了 HelloWorld.h 文件

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

回答by Max Dy

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

通过 cmd Windows 命令行转到 Java 项目的“bin”目录。

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

Then,

然后,

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