Extract and load DLL from JAR

23,032

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

Share:
23,032
Oneiros
Author by

Oneiros

I'm an Italian Programmer and Software Engineer. I love C#, Java, C++, Gaming and Virtual Reality Development

Updated on August 28, 2020

Comments

  • Oneiros
    Oneiros over 3 years

    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
    Oneiros over 13 years
    Compiler can't find FileUtils and IOUtiles
  • jmj
    jmj over 13 years
    you need to add commons-io jar in your classpath
  • jmj
    jmj over 13 years
    System.getProperty("java.library.path"); //for win machine I remember its C:\\windows\\system32
  • Oneiros
    Oneiros over 13 years
    I get Exception in thread "main" java.lang.UnsatisfiedLinkError: no j3dcore-ogl in java.library.path :(
  • jmj
    jmj over 13 years
    it simply means that the dll hasn't been extracted to lib path.
  • jmj
    jmj over 13 years
    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 .
  • Oneiros
    Oneiros over 13 years
    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...
  • jmj
    jmj over 13 years
    System.getProperty("java.library.path") it will return many path. just choose standard one and write your file there ,
  • Oneiros
    Oneiros over 13 years
    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
  • jmj
    jmj over 13 years
    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
  • Oneiros
    Oneiros over 13 years
    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
    Oneiros over 13 years