Search code examples
javaandroidmacosjava-native-interfacejavah

Javah still won't find class files in OSX / Android Studio


Having referred to --> Javah error while using it in JNI

...and --> android - javah doesn't find my class

...I still can't get javah to work. Specifically...

My java class is located here:

/Users/HKS/Code/MusicPlayer-Android/dspLibrary/src/main/java/com/company/audio/LibDSP.java`

From this folder...

/Users/HKS/Code/MusicPlayer-Android/dspLibrary/src/main/java/`

...I've tried running...

javah com.company.audio.LibDSP
javah -classpath . com.company.audio.LibDSP
javah -classpath /Applications/Android\ Studio.app/sdk/platforms/android-16/android.jar:. -jni com.company.audio.LibDSP

...I've also tried this from within the actual folder containing the .java file...

javah LibDSP
javah -classpath . LibDSP

All give the usual...

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

Any ideas?

Incidental verbose logs show something strange in the search path...

[ Search Path: /System/Library/Java/JavaVirtualMachines....../Contents/Classes/charsets.jar//Applications/Android Studio.app/sdk/platforms/android-16/android.jar:. ]

Notice how there seems to be no separator for the ones added with -classpath. I've tried pre-pending ":" and it makes no difference. I'm assuming this is a bug in the verbose output rather than the core functionality.


Solution

  • Got it! It needs the compiled class, not the source. Also, the class folder - ./build/intermediates/classes/debug is hidden from the project in Android Studio.

    This is the line that finally worked for me. From my module's root folder (/Users/HKS/Code/MusicPlayer-Android/dspLibrary/):

    javah -classpath /Applications/Android\ Studio.app/sdk/platforms/android-16/android.jar:./build/intermediates/classes/debug -jni -d src/main/jni -force com.company.audio.LibDSP
    

    Note the -d flag is the output folder, and -force ensures it overwrites any existing files.