3

I have a simple class for use on JNI, which i need to export to a header file (.h).

I've read that I need to use the javah command, by going to the src folder of the project, and type:

javah -jni com.main_package.NativeClass 

On windows it works fine, but on my main OS (Linux) it doesn't. It keeps telling that it can't find the class. I've tried to use many combinations of the command and to run it on many possible paths, but it didn't work. I've followed many tutorials and websites (including here) and tried their combinations too.

I even tried using Eclipse's external tool for this, as shown here, but it has also shown the same error.

The error I get looks like this:

error: cannot access com.main_package.NativeClass 
class file for com.main_package.NativeClass  not found
javadoc: error - Class com.main_package.NativeClass  not found.
Error: No classes were specified on the command line.  Try -help.

Can anyone please help me? I really like working on Linux and wouldn't want to switch OS each time i need to use this tool.


Here's the correct answer:

  1. for console , go to the "bin/classes" folder and run the command i've used :

    javah -jni com.main_package.NativeClass

  2. for eclipse , use the link i gave , but instead of ${project_loc}/bin/ use ${project_loc}/bin/classes/ , and then , in the package explorer , go to the NativeClass.java , and run the external tool . the output will be in the same path as of the java file .

Community
  • 1
  • 1
android developer
  • 114,585
  • 152
  • 739
  • 1,270

2 Answers2

3

You don't go to src folder of the project, but the root of your compiled .class files (where com subfolder exists or is created). javah is looking for compiled class, not your source file. If you just run javac manually, the class file will be in the same location as the original sources. Then the statement "go to src folder" is correct. However if you have a project in Eclipse or you use ant or something, the classes might be in completely different location - depending on how your project is set up. I would guess that the Eclipse projects on your Windows and on your Linux are set up differently.

Pavel Zdenek
  • 7,146
  • 1
  • 23
  • 38
  • not exactly what you wrote , but it worked in the ".../bin/classes/" folder . maybe that's because it's an android library project . how do i set it to work on eclipse itself instead of using the console? – android developer Jun 21 '12 at 11:48
  • found how to make it work on eclipse . i will update my question to include an answer. thank you. – android developer Jun 21 '12 at 11:52
  • do you know by any chance how do i use eclipse to do it and put the output file in a specific folder of the project ? – android developer Jun 21 '12 at 11:57
  • Not from the top of my head. Anyway, you better ask a new question. It is a generally useful knowledge, so why hide it in nested comments under JNI topic. – Pavel Zdenek Jun 21 '12 at 12:55
  • i've now started from scratch for eclipse on windows , and i can't find a way to do the same work using eclipse itself. it keeps saying "The file does not exist for the external tool named make using ndk." . – android developer Aug 28 '12 at 20:25
  • @androiddeveloper, when setting up my external tool to run the class with, I've pointed my "working directory" to /bin/classes/ like you stated in your update and it worked perfectly - the header file was created in the classes directory. – marienke Feb 13 '13 at 13:52
  • this question is not needed anymore , as the new NDK does all the work for you . anyway , i don't remember how to do the whole process without the NDK . – android developer Feb 13 '13 at 14:09
3

You don't have to use javah on the class files. You can use javah directly on the source files (.java) to generate the native C/++ signature (header) file. Ex:

javah -cp /Users/Android/android-sdk/platforms/android-xy/android.jar;. com.test.JniTest
VJ Vélan Solutions
  • 6,434
  • 5
  • 49
  • 63