6

Background

So I am attempting to load a jnilib (specifically JOGL) into Java on Mac OS X at runtime. I have been following along the relevant Stack Overflow questions:

The end goal for me is to package platform specific JOGL files into a JAR and unzip them into a temp directory and load them at start-up. I worked my problem back to simply attempting to load JOGL using hard-coded paths:

    File f = new File("/var/folders/+n/+nfb8NHsHiSpEh6AHMCyvE+++TI/-Tmp-/libjogl.jnilib");
    System.load(f.toString());
    f = new File ("/var/folders/+n/+nfb8NHsHiSpEh6AHMCyvE+++TI/-Tmp-/libjogl_awt.jnilib");
    System.load(f.toString());

I get the following exception when attempting to use the JOGL API:

    Exception in thread "main" java.lang.UnsatisfiedLinkError: no jogl in java.library.path

But when I specify java.library.path by adding the following JVM option:

    -Djava.library.path="/var/folders/+n/+nfb8NHsHiSpEh6AHMCyvE+++TI/-Tmp-/" 

Everything works fine.


Question

Is it possible use System.load (or some other variant) on Mac OS X as a replacement for -Djava.library.path that is invoked at runtime?

Community
  • 1
  • 1
Clinton
  • 3,638
  • 3
  • 26
  • 33

3 Answers3

2

You don't have to provide the java.library.path at startup. You can programmatically set it with

System.setProperty("java.library.path", "/var/folder/bla/foo/bar/");

I don't know if System.load() will work somehow without this library path.

David Sauter
  • 1,446
  • 11
  • 22
  • 1
    Thanks for the suggestion. However in my experience setting the 'java.library.path' at run time did not have any effect. I think this may be for similar reasons as to why you can not change the classpath at run time - http://stackoverflow.com/questions/271506/why-system-setproperty-cannot-change-the-classpath-at-run-time – Clinton Apr 06 '10 at 03:22
  • Then you might have had another problem with a dependent library. Setting the lib path at runtime works perfectly fine, I used to load my libraries just like that. – David Sauter Apr 06 '10 at 06:09
  • That's not quite true, there is a way to force re-initializing classloader with the new lib.path. – Zorkus Dec 22 '11 at 00:52
  • Do something like this: System.setProperty( "java.library.path", newPath); Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths"); fieldSysPath.setAccessible(true); fieldSysPath.set(null, null); – Zorkus Dec 22 '11 at 00:53
1

Jogl always tries to auto-load all dependent libraries. To avoid this, there should be a NativeLibLoader class where you can call disableLoading() before you load the libraries yourself via the System.load()

momania
  • 426
  • 2
  • 3
  • Sweet! Thanks for this, calling NativeLibLoader.disableLoading() before manually loading the libraries worked brilliantly. – Clinton Apr 06 '10 at 03:23
-3

System.load(...) takes libraryName as argument. It doesn't take path to library as argument. JVM searches for a library with specified name in the list of paths specified in -Djava.library.path;

Here there is nothing specific to Mac OS X. It searches for libraries in the same way on all operating systems.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Santhosh Kumar Tekuri
  • 3,012
  • 22
  • 22
  • 2
    that is wrong actually, System.load() will take the complete file name and System.loadLibrary will take the lib name – sreejith Mar 31 '10 at 08:52
  • I think loading jnilib is mac specific (it is in my case). I have it working on Linux (.so), in mac after compilation jnilib does not load. I use mvn jetty:deploy-war -Djava.library.path=/path/to/jnilib. I have even used System.setProperty("mylib.systemclassloader", "false"); so that it would run in a servlet environment (it disables automatic loading of that library). What might be the cause? – Pramod Dec 16 '11 at 04:16