How to Include Platform-Specific Native Libraries in the .Jar File Using Eclipse

How can I include platform-specific native libraries in the .JAR file using Eclipse?

For runnable JARs, what you need to do is extract to the temporary directory (maybe in a static { } block) and then load the DLL from that directory (using System.loadLibrary() in the same block). To do this, you need to subclass ClassLoader and override the findLibrary() method to allow libraries to be found in that directory. You can do whatever logic you need to here to load the particular platform libraries. To be honest, the naming for the libraries on the different platforms should be similar anyway -- I believe that you omit the 'lib' part when loading, and the extension. That's the gist of it. Probably easier to use One-JAR as the other poster mentioned :)

Exporting to Runnable jar with extra native code libraries in eclipse

Add a script containing something like that:

#!/bin/bash
java -Djava.library.path=project_lib/native/ -jar project_lib/jri.jar

I export some java projects that way.

How to load different native libraries(dll, so) based on the system the application is running

Its a little complicated, there is a full example in this loadNativeLibrary() method.

A few notes:

Try System.loadLibrary(libName) first so your library can be externalized. Most Linux packages will want to do this.

You will need to setup some method of arch and operating system native file storage in your jar. The example covers this with a few helper classes to normalize arch names. See NativeSystem.

Previous versions of Java on OS X decided that dynamic libraries used for JNI should use the jnilib extension instead of dylib like every other dynamic library on the system. You may need to work around that.

Make sure you unpack the native library to a unique file path. Its always fun when you have concurrent applications extracting different versions of your naive library to the same location and filename.

Use System.load() instead of System.loadLibrary() to load the extracted binaries. That way you don't need to worry about the java.library.path.

How to bundle a native library and a JNI library inside a JAR?

It is possible to create a single JAR file with all dependencies including the native JNI libraries for one or more platforms. The basic mechanism is to use System.load(File) to load the library instead of the typical System.loadLibrary(String) which searches the java.library.path system property. This method makes installation much simpler as the user does not have to install the JNI library on his system, at the expense, however, that all platforms might not be supported as the specific library for a platform might not be included in the single JAR file.

The process is as follows:

  • include the native JNI libraries in the JAR file at a location specific to the platform, for example at NATIVE/${os.arch}/${os.name}/libname.lib
  • create code in a static initializier of the main class to

    • calc the current os.arch and os.name
    • look for the library in the JAR file at the predefined location using Class.getResource(String)
    • if it exists, extract it to a temp file and load it with System.load(File).

I added functionality to do this for jzmq, the Java bindings of ZeroMQ (shameless plug). The code can be found here. The jzmq code uses a hybrid solution so that if an embedded library cannot be loaded, the code will revert to searching for the JNI library along the java.library.path.

How do I distribute a JOGL application?

I wrote a tutorial about how to create redistributable JOGL apps for multiple platforms. The tutorial uses Eclipse instead of NetBeans, but the principles should be the same. You can use this system to build JOGL apps that are completely self-contained, so they don't even require the user to have Java installed.

The tutorial is on my blog at http://wadeawalker.wordpress.com/2010/10/24/tutorial-creating-native-binary-executables-for-multi-platform-java-apps-with-opengl-and-eclipse-rcp/.

Exported jar from Eclipse works on Linux but not Windows

I solved the issue by using SWTJar.

SWT is a Java widget toolkit that provides access to native UI elements. This presents a problem when it comes to packaging an application as you need to include a different SWT jar for each platform (Windows/Linux/OSX)/(32/64bit). To support all of these standard platforms you have to build and distribute 6 different packages which isn't really in the spirit of Java's write once, run anywhere.

How it works:

SWTJar is an ant task which allows you to build a single jar which loads the correct SWT classes at runtime allowing you to distribute a single jar which works across (Windows/Linux/OSX)/(32/64bit).

Just in case anyone else runs into this problem! :)



Related Topics



Leave a reply



Submit