在 Java 代码中获取 java.awt.HeadlessException

Getting java.awt.HeadlessException in Java code

尝试 运行 下面的代码,我得到 java.awt.HeadlessException。问题出在哪里。

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);
    }

我收到以下错误:

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)

没有得到什么 headless 工具包被传递。我试图调试上传路径,但它的值是正确的。手动测试以及上传路径是否有效。

根据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

无头模式为False时尝试执行