Search code examples
javaseleniumdockerjenkinsawtrobot

java.awt.HeadlessException in docker jenkins


I am getting this below exception in my selenium code when I run in it my jenkins which is running through docker.

It started when I used robot class to import file for one of my scenario. When I ran my code in Jenkins it gives the below exception.

Exception: java.awt.HeadlessException: No X11 DISPLAY variable was set, but this program performed an operation which requires it. at sun.awt.HeadlessToolkit.getSystemClipboard(HeadlessToolkit.java:309) at PageObjectRep.QuickHlr.Click_on_Browser(QuickHlr.java:119) at stepDefinations.HLR_LookUp.clickOnBrowser(HLR_LookUp.java:70) at ✽.And Click on browser(Smoke.feature:178)

Note: Jenkins is running in docker. I appreciate your help in advance.

 Toolkit.getDefaultToolkit().getSystemClipboard().setContents(sel,null);
    System.out.println("selection" +sel);
    // Wait for 5 seconds
    Thread.sleep(5000);
    System.out.println("Browse button clicked");
    // Create object of Robot class
    Robot robot = new Robot();
    Thread.sleep(1000);
    // Press Enter
    robot.keyPress(KeyEvent.VK_ENTER);
    // Release Enter
    robot.keyRelease(KeyEvent.VK_ENTER);
    // Press CTRL+V
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_V);
    // Release CTRL+V
    robot.keyRelease(KeyEvent.VK_CONTROL);
    robot.keyRelease(KeyEvent.VK_V);
    Thread.sleep(1000);
    //Press Enter
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);
}

Solution

  • Quickly answer

    You can not run a GUI (Graphic User Interface) java code in an operative system without user interface like servers whose objective is run applications in background mode. You need a desktop for example.

    Detail answer

    You can divide Java in this categories :

    Java SE = Standard Edition. Commonly used for GUI or DESKTOP applications with classes like JFrame, Robot, Toolkit, etc.

    java se

    Also ,contains a lot of the libraries and APIs that any Java programmer should learn (java.lang, java.io, java.math, java.net, java.util, etc...) and are used in the next category.

    To run this applications, an operative system with user interface is required. If you run this applications in tomcat, in servers like jenkins, widfly, etc using ubuntu/debian,solaris,etc some parts will not work due to missing dependencies that only user interface operative systems has. For example, java.awt.Robot can move the mouse to x.y but in a shell server there is not mouse.

    Java EE = Enterprise Edition. The Java platform (Enterprise Edition) is used to develop distributed systems like database access (JDBC, JPA), remote method invocation (RMI), messaging (JMS), web services, XML processing, and defines standard APIs for Enterprise JavaBeans, servlets, portlets, Java Server Pages, etc...

    jee

    Currently, this category is the most used for enterprise or companies systems development: Apis rest, integrations, security, microservices, etc. Also is used to develop servers , platforms or libraries like : Tomcat, Jenkins, Pentaho, Mule ESB, Selenium, Spring, etc

    Java ME = Micro Edition. This is the platform for developing applications for mobile devices. I think this is now deprecated due to Android and other tools.