Hot questions for Using Ubuntu in jna

Question:

I am trying to call my shared library on Ubuntu on an Amazon EC2 instance The same code works 100% fine on windows but on Ubuntu - I am getting a java.lang.UnsatisfiedLinkError when I try to run it

When I run ldd on my shared library - all dependencies appear to be satisfied

root@ip-172-31-14-36:/var/lib/tomcat8/conf# ldd /tmp/dll/linux/v1/release/myConvertor.so
        linux-vdso.so.1 =>  (0x00007ffed4ffd000)
        libQt5Widgets.so.5 => /tmp/dll/linux/v1/release/libQt5Widgets.so.5 (0x00007fa81daff000)
        libQt5Gui.so.5 => /tmp/dll/linux/v1/release/libQt5Gui.so.5 (0x00007fa81d2ec000)
        libQt5Xml.so.5 => /tmp/dll/linux/v1/release/libQt5Xml.so.5 (0x00007fa81d0b0000)
        libQt5Network.so.5 => /tmp/dll/linux/v1/release/libQt5Network.so.5 (0x00007fa81cd57000)
        libQt5Core.so.5 => /tmp/dll/linux/v1/release/libQt5Core.so.5 (0x00007fa81c610000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fa81c3e6000)
        libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fa81c064000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fa81bd5a000)
        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fa81bb44000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fa81b77a000)
        libgobject-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0 (0x00007fa81b526000)
        libgthread-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgthread-2.0.so.0 (0x00007fa81b324000)
        librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fa81b11c000)
        libglib-2.0.so.0 => /lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007fa81ae0a000)
        libXext.so.6 => /usr/lib/x86_64-linux-gnu/libXext.so.6 (0x00007fa81abf8000)
        libX11.so.6 => /usr/lib/x86_64-linux-gnu/libX11.so.6 (0x00007fa81a8be000)
        libGL.so.1 => /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1 (0x00007fa81a64b000)
        libicui18n.so.54 => /usr/lib/libicui18n.so.54 (0x00007fa81a1f7000)
        libicuuc.so.54 => /usr/lib/libicuuc.so.54 (0x00007fa819e66000)
        libicudata.so.54 => /usr/lib/libicudata.so.54 (0x00007fa81843c000)
        libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fa818238000)
        /lib64/ld-linux-x86-64.so.2 (0x0000559ebb9c1000)
        libffi.so.6 => /usr/lib/x86_64-linux-gnu/libffi.so.6 (0x00007fa81802f000)
        libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007fa817dbf000)
        libxcb.so.1 => /usr/lib/x86_64-linux-gnu/libxcb.so.1 (0x00007fa817b9c000)
        libexpat.so.1 => /lib/x86_64-linux-gnu/libexpat.so.1 (0x00007fa817973000)
        libxcb-dri3.so.0 => /usr/lib/x86_64-linux-gnu/libxcb-dri3.so.0 (0x00007fa817770000)
        libxcb-present.so.0 => /usr/lib/x86_64-linux-gnu/libxcb-present.so.0 (0x00007fa81756c000)
        libxcb-sync.so.1 => /usr/lib/x86_64-linux-gnu/libxcb-sync.so.1 (0x00007fa817365000)
        libxshmfence.so.1 => /usr/lib/x86_64-linux-gnu/libxshmfence.so.1 (0x00007fa817162000)
        libglapi.so.0 => /usr/lib/x86_64-linux-gnu/libglapi.so.0 (0x00007fa816f32000)
        libXdamage.so.1 => /usr/lib/x86_64-linux-gnu/libXdamage.so.1 (0x00007fa816d2f000)
        libXfixes.so.3 => /usr/lib/x86_64-linux-gnu/libXfixes.so.3 (0x00007fa816b29000)
        libX11-xcb.so.1 => /usr/lib/x86_64-linux-gnu/libX11-xcb.so.1 (0x00007fa816926000)
        libxcb-glx.so.0 => /usr/lib/x86_64-linux-gnu/libxcb-glx.so.0 (0x00007fa81670d000)
        libxcb-dri2.so.0 => /usr/lib/x86_64-linux-gnu/libxcb-dri2.so.0 (0x00007fa816508000)
        libXxf86vm.so.1 => /usr/lib/x86_64-linux-gnu/libXxf86vm.so.1 (0x00007fa816301000)
        libdrm.so.2 => /usr/lib/x86_64-linux-gnu/libdrm.so.2 (0x00007fa8160f0000)
        libXau.so.6 => /usr/lib/x86_64-linux-gnu/libXau.so.6 (0x00007fa815eeb000)
        libXdmcp.so.6 => /usr/lib/x86_64-linux-gnu/libXdmcp.so.6 (0x00007fa815ce5000)

My code to load the shared library is as follows

String dllLocation = "/tmp/dll/linux/v1/release/myConvertor.so";
canvasConversionInterface = (CanvasConversionInterface) Native.loadLibrary(dllLocation, CanvasConversionInterface.class);

I have the following system properties set on tomcat

jna.library.path="/tmp/dll/linux/v1/release"
jna.debug_load=true

Here is the error I get when I try call that line above to load the shared library

Looking in classpath from WebappClassLoader
  context: ROOT
  delegate: false
----------> Parent Classloader:
java.net.URLClassLoader@52d278a0
 for /com/sun/jna/linux-x86-64/libjnidispatch.so
Found library resource at jar:file:/var/lib/tomcat8/webapps/ROOT/WEB-INF/lib/jna-4.5.0.jar!/com/sun/jna/linux-x86-64/libjnidispatch.so
Looking for library '/tmp/dll/linux/v1/release/myConvertor.so'
Adding paths from jna.library.path: "/tmp/dll/linux/v1/release"
Trying /tmp/dll/linux/v1/release/myConvertor.so
Adding system paths: [/usr/lib/x86_64-linux-gnu, /lib/x86_64-linux-gnu, /lib64, /usr/lib, /lib, /usr/lib/x86_64-linux-gnu/libfakeroot, /usr/lib/x86_64-linux-gnu/mesa]
Trying /tmp/dll/linux/v1/release/myConvertor.so
Looking for version variants
Looking in classpath from WebappClassLoader
  context: ROOT
  delegate: false
----------> Parent Classloader:
java.net.URLClassLoader@52d278a0
 for /tmp/dll/linux/v1/release/myConvertor.so
2017-10-02 13:27:21 [jmsNewPublicationQueueMessageListenerContainer-1] WARN  o.s.j.l.DefaultMessageListenerContainer - Execution of JMS message listener failed, and no ErrorHandler has been set.
java.lang.UnsatisfiedLinkError: Unable to load library '/tmp/dll/linux/v1/release/myConvertor.so': Native library (tmp/dll/linux/v1/release/myConvertor.so) not found in resource path ([...

...

I can verify that all paths mentioned above are accurate

Can anyone offer any ideas on what I could try next to try and get this working?

Thank you Damien


Answer:

The issue was with the shared library and it was using OpenCV After talking with the C++ developer - we figured out that the OpenCV dependency could be removed from the Library Once we removed this and tried again - everything worked perfectly

Question:

OS: Ubuntu 16.04 JNA: 4.2.2 JDK: 1.8.0u111

I'm trying to retrieve the current focused window using JNA in a JavaFX application running on Ubuntu.

To achieve my goal, I'm using X11 api, more precisely the function XGetInputFocus in that way :

if (Platform.isLinux()) {

    System.err.println("Linux platform");

    final X11 x11 = X11.INSTANCE;
    final XLib xlib = XLib.INSTANCE;

    X11.Display display = x11.XOpenDisplay(null);

    //X11.Window window = new X11.Window();
    X11.WindowByReference winRef = new X11.WindowByReference();

    Pointer pointer = Pointer.NULL;

    xlib.XGetInputFocus(display, winRef, pointer);

    /*X11.XTextProperty name = new X11.XTextProperty();
    x11.XGetWMName(display, window, name);
    System.out.println(name.toString());*/
}

-

public interface XLib extends X11 {
    XLib INSTANCE = (XLib) Native.loadLibrary("X11", XLib.class);
    //void XGetInputFocus(X11.Display display, X11.Window focus_return, Pointer revert_to_return);
    void XGetInputFocus(X11.Display display, X11.WindowByReference focus_return, Pointer revert_to_return);
}

Unfortunately, I got the following exception, I tried X11.WindowByReference and X11.Window has parameter, but it changes nothing, the exception is the same.

Linux platform
Looking in classpath from sun.misc.Launcher$AppClassLoader@73d16e93 for /com/sun/jna/linux-x86-64/libjnidispatch.so
Found library resource at jar:file:/home/puglic/eclipse/jna-4.2.2.jar!/com/sun/jna/linux-x86-64/libjnidispatch.so
Looking for library 'X11'
Adding paths from jna.library.path: null
Trying libX11.so
Found library 'X11' at libX11.so
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007f4063178e47, pid=21693, tid=0x00007f405423a700
#
# JRE version: Java(TM) SE Runtime Environment (8.0_111-b14) (build 1.8.0_111-b14)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.111-b14 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# C  [libX11.so.6+0x22e47]  XGetInputFocus+0x57

Also, It generates a huge error file named hs_err_pid21693.log. Where we can find this kind of line :

...
Internal exceptions (10 events):
Event: 0,223 Thread 0x00007f213000a000 Exception <a 'java/lang/NoSuchFieldError': method resolution failed> (0x00000000d7b0ded8) thrown at [/HUDSON3/workspace/8-2-build-linux-amd64/jdk8u111/7883/hotspot/src/share/vm/prims/methodHandles.cpp, line 1146]
Event: 0,262 Thread 0x00007f213000a000 Exception <a 'java/security/PrivilegedActionException'> (0x00000000d7b98008) thrown at [/HUDSON3/workspace/8-2-build-linux-amd64/jdk8u111/7883/hotspot/src/share/vm/prims/jvm.cpp, line 1386]
Event: 0,262 Thread 0x00007f213000a000 Exception <a 'java/security/PrivilegedActionException'> (0x00000000d7b98218) thrown at [/HUDSON3/workspace/8-2-build-linux-amd64/jdk8u111/7883/hotspot/src/share/vm/prims/jvm.cpp, line 1386]
Event: 0,263 Thread 0x00007f213000a000 Exception <a 'java/security/PrivilegedActionException'> (0x00000000d7b9a7e8) thrown at [/HUDSON3/workspace/8-2-build-linux-amd64/jdk8u111/7883/hotspot/src/share/vm/prims/jvm.cpp, line 1386]
Event: 0,263 Thread 0x00007f213000a000 Exception <a 'java/security/PrivilegedActionException'> (0x00000000d7b9a9f8) thrown at [/HUDSON3/workspace/8-2-build-linux-amd64/jdk8u111/7883/hotspot/src/share/vm/prims/jvm.cpp, line 1386]
Event: 0,263 Thread 0x00007f213000a000 Exception <a 'java/lang/NullPointerException'> (0x00000000d7b9afd0) thrown at [/HUDSON3/workspace/8-2-build-linux-amd64/jdk8u111/7883/hotspot/src/share/vm/interpreter/linkResolver.cpp, line 1178]
Event: 0,621 Thread 0x00007f21302c1800 Exception <a 'java/lang/NullPointerException'> (0x00000000d8349f68) thrown at [/HUDSON3/workspace/8-2-build-linux-amd64/jdk8u111/7883/hotspot/src/share/vm/interpreter/linkResolver.cpp, line 1178]
Event: 0,621 Thread 0x00007f21302c1800 Exception <a 'java/lang/NullPointerException'> (0x00000000d8349f68) thrown at [/HUDSON3/workspace/8-2-build-linux-amd64/jdk8u111/7883/hotspot/src/share/vm/prims/jvm.cpp, line 1390]
Event: 0,871 Thread 0x00007f21302c1800 Exception <a 'java/io/FileNotFoundException'> (0x00000000d87dc6d8) thrown at [/HUDSON3/workspace/8-2-build-linux-amd64/jdk8u111/7883/hotspot/src/share/vm/prims/jni.cpp, line 709]
Event: 1,041 Thread 0x00007f21302c1800 Implicit null exception at 0x00007f212120d176 to 0x00007f212120d531
...

Answer:

I don't think XGetInputFocus gives you the option of passing in NULL as the final argument. You need to pass in the address of a valid memory location where the function can write its revert_to "return" value.

You should use a non-null IntByReference object rather than NULL.

Question:

OS: Ubuntu 16.04 JNA: 4.2.X JDK: 1.8.0u111

I'm trying to get the currently focused window programmaticaly on a JavaFX application.

if (Platform.isLinux()) {
    final X11 x11 = X11.INSTANCE;
    final XLib xlib = XLib.INSTANCE;

    X11.Display display = x11.XOpenDisplay(null);
    X11.Window window = new X11.Window();
    Pointer pointer = Pointer.NULL;

    xlib.XGetInputFocus(display, window, pointer); // << ERROR
    X11.XTextProperty name = new X11.XTextProperty();
    x11.XGetWMName(display, window, name);
    System.out.println(name.toString());
}

public interface XLib extends StdCallLibrary {

    XLib INSTANCE = (XLib) Native.loadLibrary("/usr/lib/x86_64-linux-gnu/libX11.so", XLib.class);

    int XGetInputFocus(X11.Display display, X11.Window focus_return, Pointer revert_to_return);
}

But it doesn't work and throws this exception :

java.lang.IllegalArgumentException: Unrecognized calling convention: 63
at com.sun.jna.Native.invokeInt(Native Method)
at com.sun.jna.Function.invoke(Function.java:390)
at com.sun.jna.Function.invoke(Function.java:323)
at com.sun.jna.Library$Handler.invoke(Library.java:236)
at com.sun.proxy.$Proxy1.XGetInputFocus(Unknown Source)
at application.Main.start(Main.java:33)

Is this line correct ?

XLib INSTANCE = (XLib) Native.loadLibrary("/usr/lib/x86_64-linux-gnu/libX11.so", XLib.class);

I tested with an older version of JNA (4.1.X) and the error changed for :

Unrecognized calling convention: 1

Debug log with -Djna.debug_load=true

Looking in classpath from sun.misc.Launcher$AppClassLoader@73d16e93 for /com/sun/jna/linux-x86-64/libjnidispatch.so
Found library resource at jar:file:/home/puglic/eclipse/jna-4.2.2.jar!/com/sun/jna/linux-x86-64/libjnidispatch.so
Looking for library 'X11'
Adding paths from jna.library.path: null
Trying libX11.so
Found library 'X11' at libX11.so
Looking for library 'X11'
Adding paths from jna.library.path: null
Trying libX11.so
Found library 'X11' at libX11.so
java.lang.IllegalArgumentException: Unrecognized calling convention: 63

Answer:

Your XLib definition uses StdCallLibrary, which only makes sense on 32-bit windows systems. It should simply be Library, or derive from the JNA contrib-defined version.

You're basically asking for a calling convention that does not exist.