javajarshared-librariesonejar

Using FatJar for native libraries


I'm using FatJar for creating one jar with postgresql.jar, jdatepicker.jar and native libraries. I have combined project to one jar. There is no postgresql and jdatepicker jar error. And I can load my native library programmatically. But I can not load dependent libraries. When I put the libraries C:\Windows\System32, there is no loading dependent library error. But I want to load dependent libraries from jar. I unzipped my fatjar and I could see dependent libraries. But I can not load from jar. How can I do this ?

Edit :

This is my error output : enter image description here

and this is my classpath :

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jre7">
        <attributes>
            <attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="JavaWinForms"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="lib" path="jdatepicker-1.3.4.jar"/>
    <classpathentry kind="lib" path="postgresql-9.4-1201.jdbc41.jar"/>
    <classpathentry kind="output" path="bin"/>
</classpath>

and my native libraries are in /project/lib folder.


Solution

  • To oversimplify, you can think about the dependencies of a Java application as two separate things:

    1. The classpath. This includes your classes, as well as the jars of any libraries you're using.
    2. The native library path. This includes resources used for native code.

    It looks like you're setting the first one correctly, but not the second one.

    Without One-JAR, you'd specify your native library path using the java.library.path command-line argument. Try specifying that when you run your jar.

    If that works, then you can look into how One-JAR handles native libraries. This looks like a good starting point.

    Google is your friend, and for more information, try searching "java native libraries", "java.library.path", or "onejar native libraries".

    More info can also be found at these related questions:

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

    What is LD_LIBRARY_PATH and how to use it?

    Shameless self-promotion: You can also use a tool I created called JarMatey, which is pretty similar to One-JAR.