static { System.load("/FullPathName/myObjectFile.so"); }We probably want to use System.loadLibrary(), but I cannot get it to work. The path to the shared library file myObjectFile.so is often system dependend and I use following System call to figure it out:
// If this throws an exception, we are probably non-local System.getProperty("java.home"); // The following (commented out) seems to ALWAYS throw an exception // System.getSecurityManager().checkPropertyAccess("java.home"); String OpSys = System.getProperty("os.name"); // if local and Linux, load in a native routine to speed up write() if (OpSys.equals("Linux")) { local = true; //String libDir = "/homes/sep/matt/jag/sep/io/"; String libDir = "/homes/sep/matt/jag/sep/"; String libFile = "DataKeeper.so"; System.load(libDir + OpSys + "/" + libFile); }I guess This can also be done in a try loop that chooses to execute an alternative java implementation if the system does not supply a the required C file:
try { System.loadLibrary("file"); } catch (UnsatisfiedLinkError e) { System.err.println("can't find your library"); // do here something an alternative equivalent? }
public native void myFirstNativeMethod(...); public native int anotherNativeMethod(...);The methods have no body
method1> javah -classpath MyClassPath myPacakge.myClass method1> javah -classpath MyClassPath -stubs myPackage.myClass method1> ls myPackage_myClass.* myPackage_myClass.c myPackage_myClass.hAlternatively, use the rule in makefile.inc:
method2> make myPackage.myClass.javah
file: myCcode.c
#includeThis example does not show any variables in the package arguments Don't forget to add the variable names to the argument lists in the function declarations. The proper naming conventions#include "myPackage_myClass.h" /* Plus other #includes your code might need (e.g. stdio.h) */ extern void myPackage_myFirstNativeMethod(...) { /* your useful code goes here */ } extern int myPackage_anotherNativeMethod(...) { /* your useful code goes here */ }
example> gcc -shared myPackage_myClass.c myCcode.c \ -I/usr/local/java/include \ -I/usr/local/java/include/solaris \ -o myObjectFile.so
matt@hessen