Skip to content
Advertisement

How to load native library (.dlls) from a java application running on linux?

I have a java application in ubuntu, java application uses a jar. This jar uses some native libraries(.dll).

System.loadLibrary("my_native_library") is used in the jar to load required libraries.

In linux it tries to load an .so file means here my_native_library.so, but I have my_native_library.dll.

So I am not able to run this java application.

How should I proceed?

Advertisement

Answer

If your .jar file has included some .dll the problem is that it has several modules that use JNI (Java Native Interface) and solves something based on code that is not written in java, but in another system specific language.

Shared objects in Windows are packed in files with extension .dll while the equivalent in linux is a shared object, which is packed in a file with extension .so.xxx.yyy where xxx and yyy are numbers identifying the version of the shared object.

Linux and Windows executables are not interchangeable, what means you cannor run that .jar file in linux, most probably, except if the .jar file includes also .so files to cope with linux execution.

EDIT

Normally, if you want a JNI enabled jar file to be executable, you’ll need some installation before the jar is usable. Windows normally installs dlls in some windows directory, or if you have not done, it uses the PATH environment variable to locate the dll (so normally, programs leave dlls in the same place as the application executable) Linux has a fully documented way of searching shared objects (see ldconfig manpage for instructions, and ld.so manpage also) which involves system libraries and user shared objects.

Anyway, the jar file only uses the CLASSPATH environment to locate .class files, and you’ll need to make (extract from the jar) the shared objects visible to the system loader in use. So the best way to so install a JNI shared object, is to extract it from the .jar file and put it in a directory where it will be located by the O.S. search engine (which is different for both systems)

Beware that windows and linux JNI loaders require a common name for the library object, and they do the system dependant part to complete the shared object name. This means that for a shared object called like myshared, you’ll need to call the shared objects myshared.dll in a windows system, and myshared.so in linux (I’m not sure about this, but you’ll get it easily by trial and fail and looking the System class and System#loadLibrary(String) documentation)

Advertisement