Javah tool error: Could not find class file for hellojni

陌路散爱 提交于 2019-12-08 22:49:15

问题


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.

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.

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

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

could not find class file for hellojni

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


回答1:


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.

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

For example, assume we have the following file at 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!




回答2:


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.

See javah Documentation

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

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



回答3:


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:

javac YOUR_CLASS_FILE_NAME.java -h .

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




回答4:


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

D:/A>javah -classpath . classfilename

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




回答5:


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

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



回答6:


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

Example: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
    `


来源:https://stackoverflow.com/questions/19137201/javah-tool-error-could-not-find-class-file-for-hellojni

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!