1

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

CrazyMac
  • 462
  • 4
  • 19

1 Answers1

1

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.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • Yeah true it is Windows OS. Is there any alternative for this or should I explore? I mean in place of this method there are few other methods which I am not sure.. – CrazyMac Jun 01 '17 at 11:04
  • In fact this was my first choice. It worked good in my local machine but in Ubuntu, I started getting an exception related to ToolKit. I have updated the same in my post – CrazyMac Jun 01 '17 at 11:15
  • Now I get this error. java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment. How to set the headless = true when I run it on the server – CrazyMac Jun 01 '17 at 12:29
  • @CrazyMac Please try and google for possible solutions. There are many answered questions already on SO that answers your question, like this [SO question](https://stackoverflow.com/questions/21131855/could-not-initialize-class-sun-awt-x11graphicsenvironment-on-solaris). – Buhake Sindi Jun 01 '17 at 12:31
  • I understand, I did refer all these posts and then I am here. When I set the headless to true in the application, it throws a different error. How do I set this value in the server level. I mean this java.awt.headless=true – CrazyMac Jun 01 '17 at 12:40
  • Each server allows to add JVM arguments. I don't know what server you are running and there must be solutions out there on the web that can show you how. For now, your questions that you're asking is no longer related to the question you asked on SO. You can always post another issue by clicking the "Ask Question" button above. – Buhake Sindi Jun 01 '17 at 12:44