Home > Software design >  Getting java.awt.HeadlessException in Java code
Getting java.awt.HeadlessException in Java code

Time:06-04

Trying to run the code below and I am getting java.awt.HeadlessException. Not getting where is the issue.

public void uploadFile(String filename) throws Exception {
        clickOnAddFilesAndComment();
        WaitActions.waitForElementToBeClickable(driver, uploadButton, Utils.EXPLICIT_WAIT_VALUE_LONG);
        uploadButton.click();

        WaitActions.wait(4000);
        String testDataPath = "\\src\\test\\resources\\testFiles\\";
        String uploadPath = System.getProperty("user.dir")   testDataPath   filename;
        System.out.println(uploadPath);

        StringSelection str = new StringSelection(uploadPath);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(str, null);

        // uploadButton.sendKeys(uploadPath);
        Robot r = new Robot();
        // pressing enter
        r.keyPress(KeyEvent.VK_CONTROL);
        // releasing enter
        r.keyPress(KeyEvent.VK_V);
        // pressing ctrl v
        r.keyRelease(KeyEvent.VK_V);
        r.keyPress(KeyEvent.VK_CONTROL);
        // releasing ctrl v
        r.keyRelease(KeyEvent.VK_CONTROL);
        // pressing enter
        r.keyPress(KeyEvent.VK_ENTER);

        WaitActions.wait(Utils.LONG_WAIT_DEFAULT_MILLS);

        commentTextArea.sendKeys("test_"   filename);
        WaitActions.waitForElementToBeClickable(driver, commentButton, Utils.EXPLICIT_WAIT_VALUE_LONG);
        commentButton.click();
        WaitActions.wait(Utils.LONG_WAIT_DEFAULT_MILLS);
    }

I am getting the following error:

java.awt.HeadlessException
    at java.desktop/sun.awt.HeadlessToolkit.getSystemClipboard(HeadlessToolkit.java:216)
    at pageObjects.ShareHolderInputScreenPage.uploadFile(ShareHolderInputScreenPage.java:725)
    at stepDefinitions.ShareHolderInputScreenStepDefinitions.uploadFileAndComments(ShareHolderInputScreenStepDefinitions.java:338)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

Not getting what headless toolkit is been passed. I tried to debug the upload path but it is taking proper value. Tested that manually as well as whether that upload path is working or not.

CodePudding user response:

according to documentation

A HeadlessException is thrown when code that depends on a display device, keyboard, or mouse is called in an environment that does not support any of these

try to execute when headless mode is False

  • Related