Reputation: 11
I have researched several examples similar to this issue but i have yet to find one that is the solution to my problem. I am simply trying to do my first program using Native methods. I don't have the program stored in a hiearchy of packages because i tried to keep it as simple as possible for my first example. Here is how everything is stored:
I Have one class called NativeDemo. It is stored in C:/JavaFiles/demo/. I compiled the program and the .class file is stored in this same folder. When I try to invoke the javah command on this program it tells me the class file can't be found. Can you please tell me what I am doing wrong?
My javah command is stored in my JDK and is stored as follows: C:/jdk1.0.7_04/bin/
On the command line from the command prompt I type:
C:\JavaFiles\demo>C:\jdk1.7.0_04\bin\javah -jni NativeDemo also tried C:\JavaFiles\demo>C:\jdk1.7.0_04\bin\javah.exe -jni NativeDemo
When I do this it tells me the class file can't be found. This is the same path used when compiled and it found the file, compiled, and created the .class file with no issues.
Please help. Thanks.
Upvotes: 0
Views: 7666
Reputation: 69
in android studio 1.5 I had same problem and run above commands but nothing , I explored the app module build folder and there wasn't classes folder in intermediates folder so before run commands make the project, you can do this by Build>Make Project or shortcut key Ctrl+F9, then run above commands
Upvotes: 0
Reputation: 1
I solved this problem with the following command used in the directory of the eclipse project:
javah -classpath [PROJECT_DIR\bin\classes] class.name
Upvotes: -1
Reputation: 3224
You have to use fully qualified name for the class. The syntax for javah is javah [options] classes. classes are specified with their fully qualified names. So, in your case if ur package is demo then, the command would be,
C:\JavaFiles\demo>C:\jdk1.7.0_04\bin\javah -classpath . demo.NativeDemo
Classpath is . because, as you mentioned you have .class file in the current directory. And -jni option is not needed, cuz its default.
Upvotes: 0
Reputation: 11
As the solution was never added I thought I'd contribute the solution I found to this problem. It wasn't really a solution as such as it was 100% user error!.
In what sounds like a similar situation I wanted to create a quick small project to demonstrate the use of JNI. As this was designed to be a simple exercise I didn't bother with an IDE and simply used vi to write the code and javac to compile it.
e.g
myclass.java (fully qualified class name is mypackage.myclass)
javac myclass.java
The above command outputs myclass.class to the current directory. I now have myclass.java & myclass.class in my current working directory.
Running javah mypackage.myclass results in the error described.
The problem here is my use of javac, I wrote the compiled class to the current directory, javah however is looking for it at "/mypackage/myclass.class".
Silly little problem with a silly little answer but I was quite annoyed at wasting 15 minutes on this today so hopefully I can save someone else the same pain (and yes I know I should have spotted it sooner and may have had I not just had an entertaining few hours finding System.load discrepancies between hotspot and gnuj java implementations, so sadly I wasn't exactly trusting my tools :( !! ).
Upvotes: 1
Reputation: 719239
You probably need to supply a -classpath
parameter on the command-line to javah
to set the classpath. (The -classpath
parameter for javah
behaves the same as it does for other Java tools; e.g. java, javac, javap and so on. If you don't understand classpaths, read this page and this page.)
Upvotes: 5