16

My Java application uses a DLL library. How can I get it work from the JAR file?

The DLL is in the project's sources folder. I have to include it in my JAR, extract it at runtime (in the same directory of the jar) and load it.

Oneiros
  • 4,328
  • 6
  • 40
  • 69

1 Answers1

26

You need to put dll in your library path (recommended ) before you try to load it. so that you will have to extract it from jar and copy it into lib path .

private void loadLib(String path, String name) {
  name = System.mapLibraryName(name); // extends name with .dll, .so or .dylib
  InputStream inputStream = null;
  OutputStream outputStream = null;
  try {
    inputStream = getClass().getResourceAsStream("/" + path + name);
    File fileOut = new File("your lib path");
    outputStream = new FileOutputStream(fileOut);
    IOUtils.copy(inputStream, outputStream);

    System.load(fileOut.toString());//loading goes here
  } catch (Exception e) {
    //handle
  } finally {
    if (inputStream != null) {
      try {
        inputStream.close();
      } catch (IOException e) {
        //log
      }
    }
    if (outputStream != null) {
      try {
        outputStream.close();
      } catch (IOException e) {
        //log
      }
    }
  }

}

Note: ACWrapper is the class holding the static method

jmj
  • 237,923
  • 42
  • 401
  • 438
  • Compiler can't find FileUtils and IOUtiles – Oneiros Jan 21 '11 at 22:50
  • you need to add [commons-io](http://findjar.com/jar/commons-io/commons-io/1.4/commons-io-1.4.jar.html) jar in your classpath – jmj Jan 21 '11 at 22:52
  • 1
    `System.getProperty("java.library.path");` //for win machine I remember its `C:\\windows\\system32` – jmj Jan 21 '11 at 23:05
  • I get Exception in thread "main" java.lang.UnsatisfiedLinkError: no j3dcore-ogl in java.library.path :( – Oneiros Jan 21 '11 at 23:08
  • it simply means that the dll hasn't been extracted to lib path. – jmj Jan 21 '11 at 23:09
  • Code works.. ! try to find out whats wrong. , 1. you are making mistake while passing arg. 2. you aren't copying in some proper place 3. code is working tested . – jmj Jan 21 '11 at 23:15
  • InputStream in = Frame3D.class.getResourceAsStream("/dll/j3dcore-ogl.dll"); File fileOut = new File(System.getProperty("java.library.path")); OutputStream out = FileUtils.openOutputStream(fileOut); Can't see what's wrong... – Oneiros Jan 21 '11 at 23:19
  • `System.getProperty("java.library.path")` it will return many path. just choose standard one and write your file there , – jmj Jan 22 '11 at 07:05
  • ok now it works... but only if i run it form terminal! :( if i run the jar by double-clicking it, it freezes after loading the dll without throwing any exception... omg – Oneiros Jan 22 '11 at 13:41
  • Try from command line java -jar jarName.jar .. check if there is any excpetion . try running jar from command line from the same DIR where it is placed – jmj Jan 22 '11 at 13:50
  • yes, it works if i run with that command from the command line, without any exception... and it works also if i run it from netbeans :) i don't understand why... it's a simple HelloWorld using 3D libraries... thank you very much Jigar anyway! :D – Oneiros Jan 22 '11 at 14:04
  • solved ^^ http://stackoverflow.com/questions/4775487/java-3d-hello-world-jar-freeze – Oneiros Jan 23 '11 at 19:18