Load native libraries in an Eclipse RCP application on Linux - java

I have an Eclipse RCP application that uses some native libraries via JNI. These are shared libraries that dynamically link to each other. On Windows I put these libraries (as *.dll files) next to the RCP launcher executable (*.exe) file and load them via System.load("<absolute file path>"). This works great, as the location of the launcher seems to be added to the java.library.path so that dynamic linking between the libraries works.
On Linux, I get an UnsatisfiedLinkError. The location of the launcher is not added to the java.library.path. When I start the application from the terminal after setting the LD_LIBRARY_PATH variable it works:
export LD_LIBRARY_PATH=.
./myApp
The location . is the added to the java.library.path. Is there a better way to do this? I want the users to just double click the launcher.
Setting -Djava.library.path=. in the myApp.ini file does also not work. I see it in the installation details but I still get an UnsatisfiedLinkError.

The most reliable way to find libraries is not using java.library.path at all but finding them via Java code and load via System.load() instead of System.loadLibrary(). You can apply whatever logic you want for finding the native library (although it's probably best trying not to be too clever) and you could fall back to trying java.library.path if your mechanism fails.
This will only work of course if the library doesn't depend on other libraries that might not be found.

Related

Is it possible to get java.exe executable inside the Java.runtime folder built from javafx maven plugin

I am building an app on mac osx using javafx maven plugin. This plugin automatically creates the complete JRE inside the Java.runtime folder during the build process. But it doesnt contain the java.exe inside it.
I want to launch a jar within the app but for that I need java executable also inside the app. If I paste the java executable from system java bin folder inside the Java runtime folder of my app , I am able to launch my jar (problem gets resolved) but I am not able to launch this app to the mac app store because the Java executable is code signed with Developer ID Application: Oracle America, Inc. and has a bundle identifier of net.java.openjdk.cmd .This doesnt allow my app to be uploaded on itunes.
Is there any way out , where using javafx maven plugin I also get the java executable inside my app during the build process rahter than copying it from my system java inside my app which has a different code signature.
Instead of trying to implement system-dependent code in Java, it is better to use the facilities that are available in Java for executing a jar in the current JRE. It is possible to create a URLClassLoader https://docs.oracle.com/javase/7/docs/api/java/net/URLClassLoader.html which links to your jar (and any dependencies). From there you can use reflection to load your main class from that classloader, and execute it. There is no need to launch a new JRE (At a huge cost in RAM) unless you need to make actual changes to operating system state, such as executing SUID.
See related question if you need to execute jars with privileged operations: Signed applet loads signed jar-files using URLClassLoader with security issue

Where should I specify the native libraries for my Java application?

I know that JVM use the java.library.path property to locate the native libraries at run time. I am trying to configure Eclipse with the native dependency location.
But there are many places in Eclipse->Project Properties->Build Path I can specify the location.
The Source tab -> Native library location
The Libraries tab -> JRE System Library -> Native library location.
And for each external jars, there's a Native library location entry.
As I tried, it seems no matter where I configure it, it is always put in the java.library.path before my application starts.
Which one should I use?
If they all goes to the same destination, why so many config entries?
So far, my guess is, the designer of Eclipse hope this way can help programmers organize their native libraries more logically.
That tab within eclipse is all put onto the buildpath at runtime and is going to be organized based on the library you are putting onto the path not where in the eclipse UI you designate it.
As you mentioned it is more for a logical easier to use UI as opposed to any actual change to where it is located. Personally I prefer using the "Native Library" in the source tab but that is just out of preference.

java.lang.UnsatisfiedLinkError: no jzmq in java.library.path

I have a java application using an external dll (zmq). When I run it in debug mode in Eclipse it's all fine. However, when I export the application as Runnable JAR file then trying to run it, I'm getting that error referencing the dll.
Following my research on this site I configured the build path for JRE System Library, added that path of the folder containing the dll to Native library location, but I'm still getting that error.
Is there anything I need to do further? Or different?
Thanks.
JZMQ needs two things to run: zmq.dll (the native ZMQ library) and jzmq.dll (the "bridge" library between Java and ZMQ). The particular error you are getting means your compile environment can't locate jzmq.dll; you just need to specify the path where it can find jzmq.dll. Note that, as far as I'm aware, there is no way for jzmq.dll to be in a JAR file so you can just include it in your classpath.
Unfortunately, I can't tell you exactly how to do it in Windows. I come from Linux, where DLLs are SO files. There it would be a simple export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/jzmq.

Strange issues when loading JOGL

I've been getting this Java runtime error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no jogl in java.library.path
This is to do with having difficulty loading the dependent native libraries required to use Java OpenGL. It's an issue that's all over the net; there are some dependent files that need to be on the classpath. I've tried following the java -Djava.library.path "path/to/dir" method of registering the DLLs but it hasn't seemed to help. I think it may be because I'm trying to run this application within the Netbeans IDE.
What's odd is that I've downloaded the Netbeans JOGL plugin and their example application which shows off some basic OpenGL functions perfectly! I've searched through the code and there's no custom DLL loading, nor is there any project-specific configuration settings that I can see. I have no idea what's going on!
If you refer to jogamp.org's JOGL, please have a look at our Wiki page for IDEs.
Thx to our automated native JAR library loading,
you don't even have to set a native library path at all.
Works well w/ Eclipse - and as I heard w/ Netbeans too.
Pls don't forget to attach the source zip file to the matching gluegen/jogl
jar file, so you can benefit from our javadoc.
Check the setting of example application.
For example, in eclipse you should add jars into build path (which can be accessed from project settings)
Also u can try this plugin: http://plugins.netbeans.org/plugin/11451/addtolibrary

How to configure Eclipse project libraries to compile both on Windows and Linux?

It's a simple OpenGL app. The problem is, JOGL needs native libraries, and I need to set "Native library location", which is different for Windows and Linux. Is it possible to share project settings between platforms?
I want to make the workspace setup process as simple as "checkout from SVN, compile, run".
If it's just Windows and Linux, you can put them in the same folder, as j flemm states.
The reason it works is because of the definition of System.loadLibrary() in Java:
loadLibrary
public static void loadLibrary(String
libname)
Loads the system library
specified by the libname argument. The
manner in which a library name is
mapped to the actual system library is
system dependent.
That means System.loadLibrary("jogl") will try to load jogl.dll on Windows and libjogl.so on Linux. It's pretty nice.

Categories