Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load library from jar

how I can load a library from a jar? e.g.

Test.jar
 +- libAbc.so
 +- libDef.so
 +- ...
like image 795
Siassei Avatar asked Nov 06 '10 13:11

Siassei


People also ask

What is the difference between JAR and library?

A JAR serves the same function an an Assembly in the C#/. net world. It's a collection of java classes, a manifest, and optionally other resources, such as properties files. A library is a more abstract concept, in java, a library is usually packaged as a JAR (Java ARchive), or a collection of JARs.

Can you view a jar file?

To open a JAR file you can use any standard ZIP utility. Of course, Java being Java, we can't just call a ZIP file a ZIP file.


2 Answers

Probably not the answer you are looking for (extracting the file to a temp location is the answer you are looking for), but I thought I'd share some real world experience:

The plumbing required to exctract the library from jar, make sure it gets cleaned up afterwards, doesn't conflict with other applications that might be using the jar, etc... is very tricky. It can be done, but chances are very good that you'll wind up with either tons of temp copies of your libraries cluttering the user's system, or you'll wind up with conflicts between multiple apps using the libraries.

When you add the fact that many operating systems don't allow just any file to be used as a library (and frequently, the permissions for the user who will be running your app do not allow them to mark an arbitrary file in the temp folder for execution), the idea of packaging the native libraries inside the jar becomes less attractive.

What we finally wound up doing was shifting to a model where we have our installer (which does run with sufficient permissions) place the appropriate native library alongside the jar. This is fairly simple to do, keeps all executing libraries in the same place, and is easy to administer and understand (trying to track down a version incompatibility in one application b/c it is trying to load a library that was saved to temp storage by a second application is a total nightmare).

like image 85
Kevin Day Avatar answered Oct 14 '22 00:10

Kevin Day


Native code is being loaded by the underlying operating system, and if that code cannot peek inside a jar-file to pick up the bytes you want to load - which Windows cannot - you must make it accessible in the file system yourself.

Can you deploy your library next to your jar file?

like image 42
Thorbjørn Ravn Andersen Avatar answered Oct 14 '22 00:10

Thorbjørn Ravn Andersen