0

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.

bzkrtmurat
  • 189
  • 1
  • 3
  • 15

1 Answers1

3

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.

Community
  • 1
  • 1
Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • Thank you for reply. But I have questions. Now I'm using Eclipse Fat-Jar plugin and I'm creating fatjar. My fatjar can find my native library and it can find dependent libraries from System32 folder. But I want to find dependent libraries from my fatjar. How I can do that ? I'm missing one point. Sorry for my inexperience. I'm really newbie about creating fatjar with native dlls. – bzkrtmurat Aug 04 '15 at 14:13
  • You have to set the native library path at runtime. One-JAR (and JarMatey) do this by secretly extracting the native library files to a temporary directory and then setting the java.library.path command line argument before actually running your program. – Kevin Workman Aug 04 '15 at 14:27
  • It is only extracting my native library file. It is not extracting my native library's dependent libraries. My native library's dependent libraries are in jar file. Can I set native library path as Jar file ?? – bzkrtmurat Aug 04 '15 at 14:57
  • That's not what's happening. It's extracting the jars on your classpath, but it isn't extracting the native library files. The native library files must be loaded from the hard drive. I highly recommend trying to run this from the command line instead of trying to export it from eclipse or One-JAR, that way you can understand exactly what's going on under the hood. – Kevin Workman Aug 04 '15 at 15:00
  • I can check java.library.path with System.getProperty("java.library.path"); and one of the paths is C:\Windows\System32. I'm putting all the dependent libraries System32 and I'm runing jar from command line with "java -jar JavaWinForms_fat.jar" and my program is running perfectly. It looks like your way. You are telling that copy your dependent libraries to a folder and run your program from command line with setting java.library.path as your folder – bzkrtmurat Aug 04 '15 at 15:06
  • System32 is a default library path. If you want to include the native library files in a jar, you have to first extract them (you can't use native library files inside a jar, they have to be on the file system) and then set the java.library.path command line argument. Tools like One-JAR and JarMatey do this for you. – Kevin Workman Aug 04 '15 at 15:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/85124/discussion-between-bzkrtmurat-and-kevin-workman). – bzkrtmurat Aug 04 '15 at 15:24
  • How can I see temp file that One-JAR created for me ? Where is it ? Is it in file system ? – bzkrtmurat Aug 04 '15 at 15:56
  • I don't know how One-JAR works internally. You'd have to see their documentation or contact them. – Kevin Workman Aug 04 '15 at 16:00
  • Thank for your reply. I'm going to use Jar Matey. – bzkrtmurat Aug 04 '15 at 16:18