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

2022-01-25 00:00:00 java java-native-interface javah

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

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.

我遵循了不同的方法,甚至从 oracle 阅读了 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.

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

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

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

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

找不到 hellojni 的类文件

could not find class file for hellojni

我也尝试提供类路径,但没有得到任何头文件.

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

推荐答案

我怀疑问题是你的类有一个包,你试图从包含类文件而不是包的目录中运行命令根.

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

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

例如,假设我们在 c:srccomexampleMyClass.java

package com.example;

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

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

Go to the command line and execute the following:

c:srccomexample>javac MyClass.java

c:srccomexample>dir

 Directory of C:srccomexample

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:srccomexample>javah MyClass
Error: Could not find class file for 'MyClass'.

c:srccomexample>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

成功了!

相关文章