Re: JLink / VTK problem
- To: mathgroup at smc.vnet.net
- Subject: [mg63828] Re: [mg63825] JLink / VTK problem
- From: Todd Gayley <tgayley at wolfram.com>
- Date: Wed, 18 Jan 2006 02:38:57 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
At 01:16 AM 1/16/2006, Andy Somogyi wrote: >Hello All > >I'm trying to get Mathematica to call some VTK (visulization toolkit) >classes using the java VTK wrapper. I've tried the same example using plain >java and it works fine. > >Here is the original java code: >import vtk.*; >public class Cone { > public static void main (String []args) { > System.loadLibrary("vtkCommonJava"); > System.loadLibrary("vtkFilteringJava"); > System.loadLibrary("vtkIOJava"); > System.loadLibrary("vtkImagingJava"); > System.loadLibrary("vtkGraphicsJava"); > System.loadLibrary("vtkRenderingJava"); > > vtkConeSource cone = new vtkConeSource() >... > >And here is the Mathematica code: > >Needs["JLink`"] > >(* use the correct version of java, this is the version I build VTK with *) >In[23]:= InstallJava[CommandLine->"C:\\Program >Files\\Java\\jdk1.5.0_05\\jre\\bin\\javaw.exe"] >Out[23]=LinkObject[C:\Program Files\Java\jdk1.5.0_05\jre\bin\javaw.exe,6,2] > >(* add the location of the vtk jar *) >In[24]:=AddToClassPath["D:/src/vtk_build/bin"]; > >(* load System to create the context *) >In[25]:=LoadJavaClass["java.lang.System"] >Out[25]=JavaClass[java.lang.System,0] > >(* load the VTK native libraries, all works fine *) >In[26]:=System`loadLibrary["vtkCommonJava"] >In[27]:=System`loadLibrary["vtkFilteringJava"] >In[28]:=System`loadLibrary["vtkIOJava"] >In[29]:=System`loadLibrary["vtkImagingJava"] >In[30]:=System`loadLibrary["vtkGraphicsJava"] >In[31]:=System`loadLibrary["vtkRenderingJava"] > >(* create a new vtk object, this is where I get into trouble *) >In[32]:=cone = JavaNew["vtk.vtkConeSource"] > >From In[32]:=Java::excptn: A Java exception occurred: >java.lang.UnsatisfiedLinkError: \ >VTKInit > at vtk.vtkConeSource.VTKInit(Native Method) > at vtk.vtkObject.<init>(vtkObject.java:98) > at vtk.vtkProcessObject.<init>(vtkProcessObject.java:78) > at vtk.vtkSource.<init>(vtkSource.java:82) > at vtk.vtkPolyDataSource.<init>(vtkPolyDataSource.java:30) > at vtk.vtkConeSource.<init>(vtkConeSource.java:114) > at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native > Method) > at sun.reflect.NativeConstructorAccessorImpl.newInstance(\ >NativeConstructorAccessorImpl.java:39) > at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(\ >DelegatingConstructorAccessorImpl.java:27) > at java.lang.reflect.Constructor.newInstance(Constructor.java:494). > >From In[32]:= >JavaNew::fail: Error calling constructor for class vtk.vtkConeSource. >Out[32]=$Failed > >As you can see, the Mathematica code is doing the exact same thing as the >java code, and the java code works fine. > >BTW, I run the java example like: >javaw -classpath ../../../../bin/vtk.jar;. Cone > >Has anyone here had much experience with JLink, how reliable / stable is it? >Reason that I ask is that I'm starting to work on a binding from Mathematica >to VTK and I'm debating writting my own MathLink interface directly or using >java with the VTK java wrapper. Andy, This is a bit tricky, as it involves classloading issues. Java native libraries are loaded by a classloader. When the System.loadLibrary() Java method is called, the Java runtime has to pick a classloader to load the library. It chooses the one that loaded the class that happens to be calling System.loadLibrary(). When you execute System`loadLibrary["vtkCommonJava"] from Mathematica, the code that is calling loadLibrary() is in the internals of J/Link, and thus the native library is loaded by the classloader that loaded J/Link itself. However, when you call JavaNew["vtk.vtkConeSource"] in Mathematica, that class is loaded by the JLinkClassLoader, a special loader in J/Link that is responsible for loading all classes requested by Mathematica code. Because this is a different classloader than the one that loaded the vtkCommonJava native library, it cannot see that native library and thus you get an UnsatisfiedLinkError. J/Link gets a lot of benefit from having a special loader for all user-loaded classes (for example, the ability to dynamically add directories and jar files while Java is running), but in this case it is a drawback to have "user" code loaded by a different classloader than J/Link's internal implementation. Luckily, there is an easy fix, although you have to understand something about the internals of J/Link to recognize it. You simply write a tiny Java class with one method that calls System.loadLibrary() for you, and then load and invoke this new method from Mathematica. In this way, the loadLibrary() method is called from Java code that is loaded by the JLinkClassLoader, the same classloader that will be used when you later invoke JavaNew["vtk.vtkConeSource"]. Here is a class that does what you need: public class MyLibraryLoader { public static void loadLibrary(String name) throws Exception { System.loadLibrary(name); } } Use it from Mathematica like this: LoadJavaClass["MyLibraryLoader"]; MyLibraryLoader`loadLibrary["vtkCommonJava"] MyLibraryLoader`loadLibrary["vtkFilteringJava"] MyLibraryLoader`loadLibrary["vtkIOJava"] MyLibraryLoader`loadLibrary["vtkImagingJava"] MyLibraryLoader`loadLibrary["vtkGraphicsJava"] MyLibraryLoader`loadLibrary["vtkRenderingJava"] JavaNew["vtk.vtkConeSource"] (* This will now work. *) As for the reliability and stability of J/Link, my (unbiased) opinion is that it will not be a concern. I strongly recommend that you use J/Link instead of writing your own MathLink interface to VTK. J/Link exists precisely so that developers won't have to write their own MathLink code. Todd Gayley Wolfram Research