Javah 工具错误:找不到 hellojni 的类文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19137201/
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-12 14:26:53  来源:igfitidea点击:

Javah tool error: Could not find class file for hellojni

javajava-native-interfacejavah

提问by S.S Sahota

I am trying to create a header file using javah tool from command line on windows 7 OS but i am failing all the time.

我正在尝试从 Windows 7 操作系统的命令行使用 javah 工具创建一个头文件,但我一直都在失败。

I have followed different ways and even read the documentation of javah tool from oracle but they didn't help to overcome with this problem.

我遵循了不同的方法,甚至从 oracle 阅读了 javah 工具的文档,但它们并没有帮助解决这个问题。

My class file (hellojni.class) and java file (hellojni.java) both are in the root of D:\drive.

我的类文件 ( hellojni.class) 和 java 文件 ( hellojni.java) 都在D:\驱动器的根目录中。

But whenever I run javah tool it gives me an error:

但是每当我运行 javah 工具时,它都会给我一个错误:

could not find class file for hellojni

找不到 hellojni 的类文件

I tried by providing classpath as well but not getting any header file.

我也尝试过提供类路径,但没有获得任何头文件。

回答by Samhain

javah -classpath path_to_jars_or_classes com.my.package.MyClass.

javah -classpath path_to_jars_or_classes com.my.package.MyClass.

If you run with -verbose, javah -verbose -classpath path_to_jars_or_classes com.my.package.MyClass, it will show you the Search Path that it is using to locate your classes. You can use that to validate if your directory, D:\, is listed.

如果您使用 -verbose, 运行javah -verbose -classpath path_to_jars_or_classes com.my.package.MyClass,它将向您显示它用于定位您的类的搜索路径。您可以使用它来验证您的目录 D:\ 是否已列出。

See javah Documentation

参见javah 文档

Example: File is named MyClass.java, internal class name is MyClass. No errors.

示例:文件名为 MyClass.java,内部类名为 MyClass。没有错误。

C:\>more MyClass.java
public class MyClass
{
   public static void doSomething(int b)
   {
      return;
   }
}

C:\>javac MyClass.java

C:\>javah -classpath C:\ MyClass

C:\>dir *.h
 Volume in drive C has no label.
 Volume Serial Number is XXXX-XXXX

 Directory of C:\

10/07/2013  11:46 AM               242 MyClass.h
               1 File(s)            242 bytes
               0 Dir(s)  X bytes free

回答by Pratap Singh

Suppose your class file is in D:/A folder cd your command prompt to folder A and run below command

假设您的类文件在 D:/A 文件夹中 cd 您的命令提示符到文件夹 A 并在命令下运行

D:/A>javah -classpath . classfilename

D:/A>javah -classpath 。类文件名

Here .will set the classpath to current directory and javah tool should be able to find your class file.

这里.会将类路径设置为当前目录,javah 工具应该能够找到您的类文件。

回答by hendalst

I suspect the issue is that your class has a package and you are trying to run the command from the directory with the class file instead of the package root.

我怀疑问题在于您的类有一个包,而您正尝试从包含类文件而不是包 root 的目录运行命令

Samhain's example works because his MyClass.javacontains no package, whereas I suspect yours does.

Samhain 的示例有效,因为他的不MyClass.java包含任何包,而我怀疑你的包含。

For example, assume we have the following file at c:\src\com\example\MyClass.java

例如,假设我们有以下文件 c:\src\com\example\MyClass.java

package com.example;

public class MyClass {
    public native void myMethod();
}

Go to the command line and execute the following:

转到命令行并执行以下操作:

c:\src\com\example>javac MyClass.java

c:\src\com\example>dir

 Directory of C:\src\com\example

2015-02-23  03:17 PM    <DIR>          .
2015-02-23  03:17 PM    <DIR>          ..
2015-02-23  03:20 PM               219 MyClass.class
2015-02-23  03:17 PM                84 MyClass.java

c:\src\com\example>javah MyClass
Error: Could not find class file for 'MyClass'.

c:\src\com\example>cd c:\src

c:\src>javah com.example.MyClass

c:\src>dir
 Directory of C:\src

2015-02-23  03:18 PM    <DIR>          .
2015-02-23  03:18 PM    <DIR>          ..
2015-02-23  03:16 PM    <DIR>          com
2015-02-23  03:18 PM               449 com_example_MyClass.h

Success!

成功!

回答by Rock

On MacOS X, it required the classpath variable. This might be the solution if it can be verified on other platforms as well.

在 MacOS X 上,它需要类路径变量。如果也可以在其他平台上进行验证,这可能是解决方案。

$ javah -verbose Article.HelloJNICpp
$ javah -verbose -classpath ./ Article.HelloJNICpp
[Creating file RegularFileObject[Article_HelloJNICpp.h]]
$ 

回答by Riven666666666

If you are using eclipse: append -classpath PATH_OF_PACKAGE_TOP into javah CLASSNAME

如果您使用的是 Eclipse:将 -classpath PATH_OF_PACKAGE_TOP 附加到 javah CLASSNAME

Example:javah -classpath . com.byf.test.JNI

例子:javah -classpath . com.byf.test.JNI

my tree : `
.
├── com
│?? └── byf
│??     └── test
│??         └── JNI.java
└── libcall.so`


result:`
    byf@byf-Ubuntu:~/code/workspace_eclipse_java/JAVA_YF/src$ javah -classpath . com.byf.test.JNI
    byf@byf-Ubuntu:~/code/workspace_eclipse_java/JAVA_YF/src$ ls
    com  com_byf_test_JNI.h  libcall.so
    byf@byf-Ubuntu:~/code/workspace_eclipse_java/JAVA_YF/src$ cat com_byf_test_JNI.h 
    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
    /* Header for class com_byf_test_JNI */

    #ifndef _Included_com_byf_test_JNI
    #define _Included_com_byf_test_JNI
    #ifdef __cplusplus
    extern "C" {
    #endif
    /*
     * Class:     com_byf_test_JNI
     * Method:    add
     * Signature: (II)I
     */
    JNIEXPORT jint JNICALL Java_com_byf_test_JNI_add
      (JNIEnv *, jclass, jint, jint);

    #ifdef __cplusplus
    }
    #endif
    #endif
    `

回答by Abdel Aleem

I also faced the same problem when trying to compile and generate the C/C++ header in two steps for a Java Native Interface (JNI) implementation (I suspect that is what your trying to do from the file names). What solved it for me was to merge the two steps into one using the following command:

在尝试为 Java 本机接口 (JNI) 实现分两步编译和生成 C/C++ 头文件时,我也遇到了同样的问题(我怀疑这就是您试图从文件名中执行的操作)。为我解决的是使用以下命令将两个步骤合二为一:

javac YOUR_CLASS_FILE_NAME.java -h .

The dot (.) includes the current path. This command can only be run as of Java 8.

点 (.) 包括当前路径。此命令只能从 Java 8 开始运行。