Search code examples
javaubuntujna

Issues with screen capture using com sun JNA : undefined symbol: GetDesktopWindow


I am working on a site where I have to build a module to capture the web screen on click event. I referred few posts in SO and I tried using Robot (java.awt.Robot) and JNA (com.sun.jna.*) and both the cases I was successful when I tried from my local machine or a local tomcat server. It did capture the web page and I am using Java 1.7

When I deployed the code in my Ubuntu (Ubuntu 14.04.4), I started having challenges in both the cases.

With JNA, I keep getting the exception as mentioned below. FYI, I have mentioned the versions of Java that I am using in Ubuntu. I am sure this is something to do with configuration or I am not sure if my Java version is not picked properly by the system. Any guidance will be helpful. I referred through the internet but didnt find any guidance to solve this issue

The Java version I am using is javac 1.7.0_131 ( java-7-openjdk-amd64)

java.lang.UnsatisfiedLinkError: Error looking up function 'GetDesktopWindow': /usr/lib/jvm/default-java/bin/java: undefined symbol: GetDesktopWindow
    at com.sun.jna.Function.<init>(Function.java:212) ~[jna-4.2.2.jar:4.2.2 (b0)]
    at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:541) ~[jna-4.2.2.jar:4.2.2 (b0)]
    at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:518) ~[jna-4.2.2.jar:4.2.2 (b0)]
    at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:504) ~[jna-4.2.2.jar:4.2.2 (b0)]
    at com.sun.jna.Library$Handler.invoke(Library.java:220) ~[jna-4.2.2.jar:4.2.2 (b0)]
    at com.boot.service.$Proxy118.GetDesktopWindow(Unknown Source) ~[na:na]
    at com.boot.service.AuditService.getScreenshot(AuditService.java:133) ~[AuditService.class:na]
    at com.boot.service.AuditService.captureScreen(AuditService.java:118) ~[AuditService.class:na]
    at com.boot.service.AuditService.captureScreenshotAndStore(AuditService.java:66) ~[AuditService.class:na]
    at com.boot.controller.AuditController.captureScreenshotAndStore(AuditController.java:38) ~[AuditController.class:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_131]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_131]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_131]
    at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_131]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]

When I list my JVM folder, these are the different version of Java I see, default-java java-1.7.0-openjdk-amd64 java-7-openjdk-amd64 java-7-oracle java-8-oracle

With Robot, it worked good in my local but failed in Ubuntu with the following error.

java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11.XToolkit
    at java.lang.Class.forName0(Native Method) ~[na:1.7.0_131]
    at java.lang.Class.forName(Class.java:195) ~[na:1.7.0_131]
    at java.awt.Toolkit$2.run(Toolkit.java:869) ~[na:1.7.0_131]
    at java.security.AccessController.doPrivileged(Native Method) ~[na:1.7.0_131]
    at java.awt.Toolkit.getDefaultToolkit(Toolkit.java:861) ~[na:1.7.0_131]

I referred few posts which asked to update the headless variable to true. I am using Spring boot and not sure how to set that but I did System.setProperty("java.awt.headless", "false"); as part of the service constructor. It didnt help either


Solution

  • The GetDesktopWindow function is part of the Microsoft Windows API. Running it on a Linux environment won't work. I believe it works on your machine because your PC is a Microsoft Windows OS.


    There is an OS independent solution, here on SO that accomplishes your requirements.