I am new to java and am currently learning my own methods when I create the following files:
Main.java
public class Main {
public native int intMethod(int i);
public static void main(String[] args) {
System.loadLibrary("Main");
System.out.println(new Main().intMethod(2));
}
}
main.c
#include <jni.h>
#include "Main.h"
JNIEXPORT jint JNICALL Java_Main_intMethod(
JNIEnv *env, jobject obj, jint i) {
return i * i;
}
Compile and run :
javac Main.java
javah -jni Main
gcc -shared -fpic -o libMain.so -I${JAVA_HOME}/include \
-I${JAVA_HOME}/include/linux Main.c
java -Djava.library.path=. Main
Exit
4
But when I do this:
troller.java
public class troller {
public native int intMethod(int i);
public static void main(String[] args) {
System.loadLibrary("troller");
System.out.println(new troller().intMethod(2));
}
}
troller.c
#include <jni.h>
#include "troller.h"
JNIEXPORT jint JNICALL Java_Main_intMethod(
JNIEnv *env, jobject obj, jint i) {
return i * i;
}
Compile and run :
javac troller.java
javah -jni troller
gcc -shared -fpic -o libMain.so -I${JAVA_HOME}/include \
-I${JAVA_HOME}/include/linux Main.c
java -Djava.library.path=. troller
Exit
Exception in thread "main" java.lang.UnsatisfiedLinkError: no troller in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1886)
at java.lang.Runtime.loadLibrary0(Runtime.java:849)
at java.lang.System.loadLibrary(System.java:1088)
at troller.main(troller.java:4)
Why does the program stop working if I change the name?
EDIT : After examining the answers, I noticed some inconsistencies and now recreated all the files, but the error persists:
Native.java
public class Native{
public native int intMethod(int i);
public static void main(String[] args){S
System.loadLibrary("Native");
System.out.println("In java... :)");
System.out.println(new Native().intMethod(4));
}
}
Native.c
#include<jni.h>
#include"Native.h"
JNIEXPORT jint JNICALL Java_Native_intMethod(
JNIEnv *env, jobject obj, jint i){
printf("In C .... :)");
return i*i;
}
compile and run
javac Native.java
javah -jni Native
gcc -shared -fpic -o libMain.so -I${JAVA_HOME}/include \
-I${JAVA_HOME}/include/linux Native.c
java -Djava.library.path=. Native
Exception in thread "main" java.lang.UnsatisfiedLinkError: no Native in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1886)
at java.lang.Runtime.loadLibrary0(Runtime.java:849)
at java.lang.System.loadLibrary(System.java:1088)
at Native.main(Native.java:4)