3

The first difficulty in using JNA with J9 is that the J9 JVM does not include the java.awt package and the Native class imports a few classes from this package. This is easily overcome by downloading the JNA source, ripping out these imports and their dependent methods (which I am not using anyway), and building a new JNA jar.

Here is a simple test program:

    public class TestJni {
        public static void main(String[] args) {
            CLibrary instance = (CLibrary) Native.loadLibrary((Platform.isWindows() ? "msvcrt" : "c"), CLibrary.class);     
            instance.printf("Hello, World\n", new Object[] {});
        }

        // This is the standard, stable way of mapping, which supports extensive
        // customization and mapping of Java to native types.
        public interface CLibrary extends Library {        
            void printf(String format, Object[] args);
        }
    }

After correcting the java.awt problem, I receive the error:

Caused by: java.lang.UnsatisfiedLinkError: C:\DOCUME~1\TSO0112\LOCALS~1\Temp\jna72681.dll (Incompatible JNI version (not 1.1, 1.2 or 1.4))
at java.lang.ClassLoader.loadLibraryWithPath(ClassLoader.java:973)
at java.lang.System.load(System.java:459)
at com.sun.jna.Native.loadNativeLibraryFromJar(Native.java:696)
at com.sun.jna.Native.loadNativeLibrary(Native.java:620)
at com.sun.jna.Native.<clinit>(Native.java:104)
at java.lang.J9VMInternals.initializeImpl(Native Method)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:187)
at TestJni.main(TestJni.java:8)

What does it mean by "Incompatible JNI version"? Has anybody out there got J9 to play nice with JNA?

UPDATE: I think JNA is suppressing the following NoClassDefFoundError on trying to load the java.nio.Buffer class because J9 apparently does not have the NIO package included:

JNA: Problems loading core IDs: java.nio.Buffer
Exception in thread "main" java.lang.NoClassDefFoundError: java.nio.Buffer
    at java.lang.ClassLoader.loadLibraryWithPath(Native Method)
    at java.lang.ClassLoader.loadLibraryWithPath(ClassLoader.java:965)
    at java.lang.System.load(System.java:459)
    at TestJni.main(TestJni.java:8)
Jonathan Schneider
  • 26,852
  • 13
  • 75
  • 99

2 Answers2

1

I have been working with JNA on j9 for a couple of months now. I have had a few small niggles here and there but things mostly seem working fine.

First - The latest versions of JNA(3.2.7) seem to import awt. I am using 3.2.4 and the awt imports are commented out. I think that will work out of the box for you.

Second - THe version of J9 i am working with is for WinCE and it is a JVM for java 1.4. Latest JNA though is built off of java 1.5. So you might want to check which version of java your version of j9 is built on. JNA 3.2.4 is compatible with java 1.4 I believe.

Abhijith
  • 2,592
  • 5
  • 18
  • 30
  • Thanks for the response. I tried to load the temp jnaXXXXXX.dll directly yesterday using System.load() and it blew up on trying to accessing java.nio.Buffer. It appears that J9 does not have the NIO package included. I think JNA is hiding this error, and throwing the UnsatisfiedLinkError. Have you encountered this problem as well? – Jonathan Schneider May 20 '11 at 13:46
  • Yes NIO has been excluded from j9. If you look at the native code in JNA , it is actually calling System.Load to load the Dlls. – Abhijith May 20 '11 at 14:05
  • You are absolutely right. If you look at the above stack trace, you will find that System.load() itself references the NIO package! How are you getting around this successfully? BTW great suggestion on moving back to JNA 3.2.4 -- I am much more comfortable with this than ripping stuff out of the 3.2.7 jar manually. – Jonathan Schneider May 20 '11 at 17:13
  • I've actually not observed this but yes NIO seems to be causing problems.There doesn't seem to be an easy way around this. I got my sources from http://java.net/jira/browse/JNA-108 . This a wince port for JNA. The author has already commented out all the NIO stuff and awt stuff . I guess you could try that. – Abhijith May 20 '11 at 17:26
  • Great find. I can see this working for WinCE, but it still blows up on the same line when trying to run on win32-x86. I am trying to get JNA work with the J9 JVM included in Sametime 7.5.1. I am beginning to think it isn't possible. Will still mark this answer as accepted, as I think it will be greatly beneficial to folks trying to accomplish the same thing on Windows Mobile devices. – Jonathan Schneider May 20 '11 at 19:46
0

You can also simply provide your own stub implementations of the java.nio/java.awt stuff and simply avoid using those features (mainly direct buffer stuff and obtaining a handle to a native window).

technomage
  • 9,861
  • 2
  • 26
  • 40
  • Oh believe me, I tried... easier said than done. Primarily because JNA actually does need the Buffer class. – Jonathan Schneider Sep 02 '11 at 20:37
  • I'm not sure what you mean. JNA refers to java.nio.Buffer and java.awt, but if you include your own (empty) implementations that precede JNA in the classpath, everything should run just fine. – technomage Oct 04 '11 at 15:22