5

I have a Java program making some JNI calls to a native library ("mylib.so"). Whenever I want to run this program, from the command line, I must set java.library.path to the location of my library as such:

java -Djava.library.path=/var/natives/ -classpath MyPackage.jar MyPackage.MyClass arg1 arg2

I'm wondering if there are any alternatives so I do not have to set it with the -D option everytime I run my program.

I have tried adding /var/natives/ to my $PATH variable, but it still complains that it cannot find the library if I do not explicitly set it with -D.

Do I have any other options?

JoshDM
  • 4,939
  • 7
  • 43
  • 72
Petey B
  • 11,439
  • 25
  • 81
  • 101

4 Answers4

2

Just put the entire command in a .sh file to save yourself from repeating it everytime.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
1

Instead of using System.loadLibrary("mylib"), use System.load("/var/natives/mylib.so").

Or, you could define a custom class loader for the class and override the ClassLoader.findLibrary(String) method.

Kevin K
  • 9,344
  • 3
  • 37
  • 62
0

One place you can put it (which admittedly may be suboptimal) is in the [JRE]/lib/i386 directory (or [JRE]/lib/x64 or whatever it is called in a 64-bit Java installation).

Also, have you tried putting /var/natives in LD_LIBRARY_PATH?

QuantumMechanic
  • 13,795
  • 4
  • 45
  • 66
0

Another possibility is creating your own variable with the switches in it. For example:

COMPILE="-Djava.library.path=/var/natives/ -classpath /var/packages/MyPackage.jar"

Then run the command like:

java $COMPILE MyPackage.MyClass arg1 arg2 arg3
Petey B
  • 11,439
  • 25
  • 81
  • 101