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
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, 2020Comments
-
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 over 13 yearsCompiler can't find FileUtils and IOUtiles
-
jmj over 13 yearsyou need to add commons-io jar in your classpath
-
jmj over 13 years
System.getProperty("java.library.path");
//for win machine I remember itsC:\\windows\\system32
-
Oneiros over 13 yearsI get Exception in thread "main" java.lang.UnsatisfiedLinkError: no j3dcore-ogl in java.library.path :(
-
jmj over 13 yearsit simply means that the dll hasn't been extracted to lib path.
-
jmj over 13 yearsCode 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 over 13 yearsInputStream 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 over 13 years
System.getProperty("java.library.path")
it will return many path. just choose standard one and write your file there , -
Oneiros over 13 yearsok 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 over 13 yearsTry 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 over 13 yearsyes, 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 over 13 yearssolved ^^ stackoverflow.com/questions/4775487/…