2
votes

I am trying to get a Java method to work via J/Link from Mathematica. The problem is that the method calls a JNI (.dll file). Outside of Mathematica from the command line, all works great. But calling the method within Mathematica, the JVM returns the following:

Java::excptn: A Java exception occurred: java.lang.UnsatisfiedLinkError: ncsa.hdf.hdf5lib.H5.H5Fcreate(Ljava/lang/String;III)I
    at ncsa.hdf.hdf5lib.H5.H5Fcreate(Native Method)
    at ncsa.hdf.object.h5.H5File.createFile(H5File.java:920)
    at H5FileCreateFromMathematica.H5FileCreate(H5FileCreateFromMathematica.java:38).

I have spent a lot of time trying to get everything right with classpath and path in various permutations. This might still be the problem, though I feel I have worked hard to rule out these possibilities.

Any insights onto (1) what this message from Java means and even better (2) how to fix it would be much appreciated.

  • EDIT: FURTHER INFORMATION, after implemeneting Artefacto's very helpful suggestion * The result of Artefacto's input is a change in the error message. On first call, fileFormat@create["C:\Test"] yields:

Java::excptn: A Java exception occurred: java.lang.UnsatisfiedLinkError: Native Library C:\MyJava\hdf-java\lib\win\jhdf5.dll already loaded in another classloader at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1772) at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1732) at java.lang.Runtime.loadLibrary0(Runtime.java:823) at java.lang.System.loadLibrary(System.java:1028) at ncsa.hdf.hdf5lib.H5.<clinit>(H5.java:232) at ncsa.hdf.object.h5.H5File.createFile(H5File.java:920) at ncsa.hdf.object.FileFormat.create(FileFormat.java:1472).

Calling immediately again, fileFormat@create["C:\Test"] yields:

Java::excptn: A Java exception occurred: java.lang.NoClassDefFoundError: Could not initialize class ncsa.hdf.hdf5lib.H5 at ncsa.hdf.object.h5.H5File.createFile(H5File.java:920) at ncsa.hdf.object.FileFormat.create(FileFormat.java:1472).

Any help in the interpretation of these changes and what it might mean for getting this to work in Mathematica?

2

2 Answers

2
votes

Java is looking for the native library but not finding it. According to the forum post linked below, you should set the "com.wolfram.jlink.libdir" system property or the JLINK_LIB_DIR environment variable. This may be configuring where it should look for JLink itself so you might also try to put the required library where the JLinkNativeLibrary.dll file lives in the Mathematica installation layout.

This is the related forum thread: http://forums.wolfram.com/mathgroup/archive/2008/Aug/msg00664.html

2
votes

Try passing the JVM, in the command line, the path of the dll directory, like so:

InstallJava[CommandLine-> "c:\\path\\to\\java.exe -Djava.library.path=c:\\path\\to\\libdirectory"]

Then call Runtime.getRuntime().loadLibrary("mylib");:

LoadJavaClass["java.lang.Runtime"];
runtime = java`lang`Runtime`getRuntime[];
process = runtime@loadLibrary["mylib"];