javaclinuxjava-native-interfacejavah

Using native code in java


I want to create c library and use it in my java code on an Linux OS. I'm trying to understand and implement natural library concept. I'm following this tutorial http://diglib.stanford.edu:8091/~testbed/doc/JavaUsage/JNI/tutorial.txt

Which is helpful me to understand concept a little. However, I get errors when I try to do it myself. I searced for errors I am getting but none of solutions helped.

Main class code and class for natural library I wrote is as follows:

package natLib;
import natLib.getKeyPressed;
public class main {
public static void main(String[] args) {
    getKeyPressed natlab=new getKeyPressed();
    char c=natlab.keyboardPressedKey();

}
}

package natLib;    
public class getKeyPressed {
static {
 System.loadLibrary("natlab");
    }
public native char keyboardPressedKey();
}

when I write "javac main.java" I get errors like

"main.java:6: error: cannot find symbol getKeyPressed natlab=new getKeyPressed();"

And when I skip for main and just do javac prcess for class with native method, try to obtain a header file javah -jni getKeyPressed.class

Although there is a file as getKeyPressed.class, I get errors like:

"Exception in thread "main" java.lang.IllegalArgumentException: Not a valid class name:     getKeyPressed.class"

I try it without .class extention it says

"Error: Could not find class file for 'getKeyPressed'."

It says that even when I make getKeyPressed class file by copying getKeyPressed.class.

It seems I am making a major mistake, any suggestions to solve this?


Solution

  • javah expects a fully qualified classname. (e.g. natLib.getKeyPressed, not just getKeyPressed)