java cannot load dll outside jre - java

Why it seems that is impossible to load a library (.dll) in Java, if it is not located in /jre/bin folder. It's crazy, but I have 2 dll's:
aa.dll
bb.dll, which depends on aa.dll
If I copy these dll's in C:\Tmp the following code it is not working.....
If copy these dll'd in C:\Program Files\Java\jre1.8.0_25\bin, the same code it's working.
The code, very simple:
public class Main {
public static void main(String[] args) {
System.load("C:\\Tmp\\ftd2xx.dll");
System.out.println("ftd2xx.dll loaded");
System.load("C:\\Tmp\\JD2XX.dll");
}
}
The error (result):
ftd2xx.dll loaded
Exception in thread "main" java.lang.UnsatisfiedLinkError: no jd2xx in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at jd2xx.JD2XX.<clinit>(JD2XX.java:729)
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(Unknown Source)
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.load0(Unknown Source)
at java.lang.System.load(Unknown Source)
at Main_02.main(Main_02.java:7)
To be more confusing, one library I can load from c:\Tmp (aa.dll), the other one I cannot (bb.dll).
This last one requires to reside in C:\Program Files\Java\jre1.8.0_25\bin.
If somebody has any useful idea, I will appreciate very much.
I tought the JVM does not want to load possible dangerous, or untrusted native code? And if it resides in the jre folder it consider the code is safe? But it is jus an ideea.
Thank you.

I confirm the same:
Windows 7(64bit);jre1.8.0_102(32bit);DLL's(32bit);
Exactly the same but in my case one dll(aa.dll) is dependent on another bb.dll (aa.dll->bb.dll)
aa.dll can be loaded from anywhere (a projects lib path for ex), but only and only if I copy bb.dll to jre1.8.0_102\bin directory; No chances to load otherwise. Tried various combinations to set -Djava.library.path=project_path\lib, to set system PATH=project_path\lib, copy both dll's to windows\system32 - nothing helps, the only chance is to have it on jre\bin path.
AN UPDATE:
And you have answered your question your self. It's a security:
Go to -> Java Control Panel -> Advanced -> Mixed code ..
Set "Disable verification.."
and aa.dll loads successfully. Both stored in project_path\lib

Related

How to prevent "No jhdf5 in java.library.path" error in runnable JAR

I have a Java program that reads and processes HDF5 files. I got the below error when I ran it on Eclipse (windows OS), and had to add a run configuration VM argument (-Dncsa.hdf.hdf5lib.H5.hdf5lib=\jhdf5.dll) to add the library and run properly. Now I'm exporting a runnable Jar file I get the error again, likely because it doesn't inherit the VM argument.
java.lang.UnsatisfiedLinkError: no jhdf5 in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at ncsa.hdf.hdf5lib.H5.loadH5Lib(H5.java:347)
at ncsa.hdf.hdf5lib.H5.<clinit>(H5.java:274)
at ncsa.hdf.hdf5lib.HDF5Constants.<clinit>(HDF5Constants.java:28)
at ncsa.hdf.object.h5.H5File.<init>(H5File.java:99)
at ncsa.hdf.object.h5.H5File.<init>(H5File.java:132)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at ncsa.hdf.object.FileFormat.<clinit>(FileFormat.java:229)
at hdf5Tool.HDF5Interface.readNodeStructureH5(HDF5Interface.java:73)
at wizardPages.DREAMWizard$STORMData$1.run(DREAMWizard.java:340)
at org.eclipse.jface.operation.ModalContext$ModalContextThread.run(ModalContext.java:122)
Based on this post, I see that two possible options are to create a shell script or application bundle to incorporate the VM argument.
However, I'm curious is there is a more simple way to identify the library for the runnable JAR file. Any suggestions?
Not sure if you have specific needs, but maybe check out jhdf.io it's a pure Java HDF5 library so doesn't require native libraries to be loaded. It should be simple to add to a Maven or Gradle project with standard dependencies.
It's quite an early stage but should support reading data from most HDF5 file, if it doesn't work open an issue.
Disclaimer: I am the author of jhdf so might be biased.

How to build native libraries for Iotivity in order to load them in Java

I'm trying to run Iotivity Simple Clent in Java. I have cloned Iotivity project https://github.com/iotivity/iotivity and I have created an eclipse project that includes all the required Iotivity Java classes plus the classes for the SimpleClient from java-examples folder and I have no error so far.
When I try to run SimpleClient.java I get
Exception in thread "main" java.lang.UnsatisfiedLinkError: no ocstack-jni in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at org.iotivity.base.OcPlatform.<clinit>(OcPlatform.java:39)
at org.iotivity.base.examples.SimpleClient.startSimpleClient(SimpleClient.java:75)
at org.iotivity.base.examples.SimpleClient.main(SimpleClient.java:524)
I have installed SCONs and Gradle but I don't know how to proceed in order to build and create the "ocstack-jni" or any other library that is required to be loaded. My operating system is Windows 10.
You need to build with BUILD_JAVA=1 and the JAVA_HOME environment var must not be pointing to your current java install.

Java error no lwjgl64 in path?

I'm trying to make a game and it runs fine in eclipse, but I get this error when I export and run it as a jar file.
Exception in thread "main" java.lang.UnsatisfiedLinkError: no lwjgl64 in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at org.lwjgl.Sys$1.run(Sys.java:72)
at java.security.AccessController.doPrivileged(Native Method)
at org.lwjgl.Sys.doLoadLibrary(Sys.java:66)
at org.lwjgl.Sys.loadLibrary(Sys.java:87)
at org.lwjgl.Sys.<clinit>(Sys.java:117)
at org.lwjgl.opengl.Display.<clinit>(Display.java:135)
at org.newdawn.slick.AppGameContainer$1.run(AppGameContainer.java:39)
at java.security.AccessController.doPrivileged(Native Method)
at org.newdawn.slick.AppGameContainer.<clinit>(AppGameContainer.java:36)
at ultra.game.core.MainGame.main(MainGame.java:1827)
I have tried so many things. I set the natives location to the native folder and I checked inside and lwjgl64 is in there. Any help?
LWJGL uses its own variables for the path to the native libraries:
System.setProperty("org.lwjgl.librarypath", new File("pathToNatives").getAbsolutePath());
If you kept the file structure from the LWJGL package you can use something like this:
switch(LWJGLUtil.getPlatform())
{
case LWJGLUtil.PLATFORM_WINDOWS:
{
JGLLib = new File("./native/windows/");
}
break;
case LWJGLUtil.PLATFORM_LINUX:
{
JGLLib = new File("./native/linux/");
}
break;
case LWJGLUtil.PLATFORM_MACOSX:
{
JGLLib = new File("./native/macosx/");
}
break;
}
System.setProperty("org.lwjgl.librarypath", JGLLib.getAbsolutePath());
This means that you do not have the native library named "lwjgl64" in a place so that Java can find and load it, or that you are using a 32-bit JVM and you are trying to load a 64-bit native library (or vice versa) - if you want to use native libraries, they must have the same "bitness" as the JVM you are using.
On Windows, the native library would be in a file lwjgl64.dll; on Mac OS X or other Unix-like systems it would be lwjgl64.so. Find this file, and then set the system property java.library.path using the -D option when you start your program, for example:
java -Djava.library.path=C:\MyProject\lib com.mypackage.MyProgram
where C:\MyProject\lib would be the directory that contains the DLL.

7Zip-JBinding UnsatisfiedLinkError

I've looked through a number of the UnsatisfiedLinkError threads on here and nothing seems to quite fit my problem.
I am trying to get this api working: 7ZipBind
However I am having an issue, and cant get the thing to work. I am running some of its example code from the commandline like so:
C:>java -classpath
"/sevenzipTest.jar;/sevenzipjbinding-AllWindows.jar;\sevenzipjbinding.jar"
com.poc.sevenzip.SevenZipSimple c:\archive\test.zip
However when I try to run it I get the error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: D:\Users\XXXX\AppData\Local\Temp\SevenZipJBinding-9640553\lib7-Zip-JBinding.dll: Can't
find dependent libraries
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(Unknown Source)
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.load0(Unknown Source)
at java.lang.System.load(Unknown Source)
at net.sf.sevenzipjbinding.SevenZip.initSevenZipFromPlatformJARIntern(SevenZip.java:428)
at net.sf.sevenzipjbinding.SevenZip.initSevenZipFromPlatformJAR(SevenZip.java:235)
at net.sf.sevenzipjbinding.SevenZip.ensureLibraryIsInitialized(SevenZip.java:578)
at net.sf.sevenzipjbinding.SevenZip.openInArchive(SevenZip.java:567)
at com.poc.sevenzip.SevenZipSimple.runZip(SevenZipSimple.java:25)
at com.poc.sevenzip.SevenZipSimple.main(SevenZipSimple.java:64
What I am confused about, is why it says it cant locate the libraries (dll files), when the libraries are stored within the <file_path>/sevenzipjbinding-AllWindows.jar file... I am obviously not doing something correct here, and was hoping some1 can give me some pointers.
Cheers
The bug fix was released. Please, use version 1.06rc or above.
Download URL:
All versions
4.65-1.06rc-extr-only
The following instructions are for Windows x86 and 7 Zip JBinding version 4.65-1.05 RC. Download the following DLLs.
libstdc++-6.dll
libgcc_s_dw2-1.dll
gpsvc.dll
sysntfy.dll
Add the DLLs into the Windows-x86 directory in sevenzipjbinding-AllPlatforms.jar. Then append the following lines to sevenzipjbinding-lib.properties in the Windows-x86 directory in the jar.
sevenzipjbinding.libname.3=gpsvc.dll
sevenzipjbinding.libname.4=libgcc_s_dw2-1.dll
sevenzipjbinding.libname.5=libstdc++-6.dll
sevenzipjbinding.libname.6=sysntfy.dll
Instead of modifying the jar file, one could put the DLLs in the PATH (not classpath).
In order to figure this out, I extracted lib7-Zip-JBinding.dll from sevenzipjbinding-AllPlatforms.jar. I then opened the file in Dependency Walker.

Java - Library path error

I'm trying to start my java game but I have some troubles with the java command line:
Here is what I type:
C:\>java -Djava.library.path=%cd%\lib -jar game.jar
And here is what I got:
Exception in thread "main" java.lang.NoClassDefFoundError: org/lwjgl/LWJGLException
at com.game.Main.main(Main.java:7)
Caused by: java.lang.ClassNotFoundException: org.lwjgl.LWJGLException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
I can start my game with Eclipse (With Run as Java Application) but after the menu I got the following error:
Exception in thread "Thread-5" java.lang.UnsatisfiedLinkError: no lwjgl in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at org.lwjgl.Sys$1.run(Sys.java:72)
at java.security.AccessController.doPrivileged(Native Method)
at org.lwjgl.Sys.doLoadLibrary(Sys.java:65)
at org.lwjgl.Sys.loadLibrary(Sys.java:81)
at org.lwjgl.Sys.<clinit>(Sys.java:98)
at org.lwjgl.opengl.Display.<clinit>(Display.java:129)
at com.game.displaygui.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
lwjgl.jar has been put into the folder \lib.
Could you explain me with I got that?
Thanks.
This because lwjgl library is made by two components:
the .jar file which contains Java code
and the native binary library (which can be .so or .dll or .dylib according to your OS)
The first error you are getting is because you are setting the library path, that should contain the native library, but it does contain the .jar. So you get a java.lang.NoClassDefFoundError because you should set either the library path to the folder that contains native library, either the classpath to contain the real lwjgl.jar file.
The second error that you get with Eclipse is a successive step: your classpath contains the jar library but it is not able to find the native library attached to it, you can fix it in the following way:
You should specify explicitly which lib files to include (seperated by ;) :
-cp %cd%\lib\lwjdl.jar;%cd%\lib\<another-lib>.jar
Don't be tempted to use the wildcard * as it will cause more harm than good (from previous experience :))
You use the java.library.path option to specify the location of native libraries to load. In the location pointed to by that option you would place all dll or .so files required by LWJGL. On the other hand, you need to make sure that all required JAR files are on your classpath, via the -classpath option. Currently, you have your JAR files in the wrong directory.
In Eclipse, if you using maven.
Add the following to the project "Run" : "VM options" .
-Djava.library.path=yourpath/youproject/java/target/natives

Categories