I was just trying to make a toy program to bind native code to java just for fun. I have successfully been able to make the program run using System.load("/FULLPATH/mylib.so"), but having trouble loading libraries specifically from java.library.path using System.loadLibrary().
Tools I'm using:
- gcc 4.8.5 20150623 (Red Hat 4.8.5-4)
- java/javac version "1.8.0_66" (HotSpot 64 bit)
- CentOS 7 (64 bit)
Build sequence (same for both versions mentioned in first paragraph).
rm TestIt.class mylib.so TestIt.h javac TestIt.java javah -stubs TestIt gcc -shared -I/$JDK8_HOME/include/ -I/$JDK8_HOME/include/linux/ -fPIC nativeTestItImpl.c -o mylib.so
Java code to load the library (simply a static initializer):
static {
System.out.println("System.getProperty(\"java.library.path\") is: " + System.getProperty("java.library.path"));
System.loadLibrary("mylib");
//System.load("/FULLPATH/mylib.so");// ***This works***
}
How I've tried to set the library path:
java -Djava.library.path=/FULLPATH TestIt java -Djava.library.path=/FULLPATH/mylib.so TestIt java -Djava.library.path=. TestIt #appeared as '.' java -Djava.library.path=/FULLPATH:$PATH TestIt #NOTE: DEFLT_LIB_PATH was the output of the above print statement when # running "java TestIt" java -Djava.library.path=/FULLPATH:$DEFLT_LIB_PATH TestIt set LD_LIBRARY_PATH=/FULLPATH && java TestIt ##did not appear in printout of lib export LD_LIBRARY_PATH=/FULLPATH && java TestIt ##preppended /FULLPATH: to DEFLT_LIB_PATH
All version printed out as, expected exception those labeled with the "##" above.
Error in all cases:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no mylib in java.library.path
Full path has no spaces or special characters so I'm quite lost why this is the case.
Also have tried System.loadLibrary("mylib.so"); just for completeness.