JavaFX apllication does not run with Selenium WebD

2019-05-21 01:28发布

问题:

I want to use Selenium WebDriver in JavaFX application that I want to get a screenshot of a webpage and show it in ImageView in JavaFX app.

This code works completely fine to take screenshot of webpage and save it:

package application;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;

public class Main{

    public static void main(String[] args) throws Exception {

        File file = new File("E:\\Eclipse_Projects\\phantomjs-2.1.1-windows\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
        System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.14.0-win64\\geckodriver.exe");
        System.setProperty("phantomjs.binary.path", file.getAbsolutePath());

        WebDriver driver = new PhantomJSDriver();
        driver.get("http://google.com");

        // taking screenshot
        File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(screenshot, new File("screenshot.png"));

        driver.close();

        System.out.println("Done!");    
    }
}

In order to use the saved screenshot in JavaFX app I tried to make some simple modifications to this class to make it a JavaFX app.

Here is the modified code:

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;

import javafx.application.Application;
import javafx.stage.Stage;

public class Main extends Application{

    public static void main(String[] args) throws Exception {

        File file = new File("E:\\Eclipse_Projects\\phantomjs-2.1.1-windows\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
        System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.14.0-win64\\geckodriver.exe");
        System.setProperty("phantomjs.binary.path", file.getAbsolutePath());

        WebDriver driver = new PhantomJSDriver();
        driver.get("http://google.com");

        // taking screenshot
        File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(screenshot, new File("screenshot.png"));

        driver.close();

        System.out.println("Done!");

        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        primaryStage.show();
    }
}

This is one of the simplest JavaFX applications with some code in main method and when I run it, it should save the screenshot and open a blank JavaFX app. But when I run it, it does not do anything, it does not even execute the main method. It gets terminated after 1-2 seconds without doing anything.

Then I realized that JavaFX application does not run when Selenium Webdriver library is in the build path. That even if I delete all Selenium stuff from the class, it ends up with the same problem.

package application;

import javafx.application.Application;
import javafx.stage.Stage;

public class Main extends Application{

    public static void main(String[] args) throws Exception {

        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        primaryStage.show();
    }
}

The code above runs only when I delete Selenium WebDriver library from the project's build path.

I have tried these in two IDEs: In Eclipse, the program gets terminated after 1-2 seconds without doing anything and without any error. In NetBeans, the program gets terminated after 1-2 seconds without doing anything but it gives me an error:

C:\Users\User\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: -1073740791
BUILD FAILED (total time: 2 seconds)

It is almost 2 years that I am writing code in Java, but it is the first time that I come across such a problem. I googled a lot and didn't find anything similar to this.

Does anyone have an idea what is going on here and what is the problem? Any idea would be appreciated.

Edit: it works normally when I run the generated jar-file outside of IDE.

Thanks for consideration.

回答1:

I just found the reason of this issue. The problem is related to the NVIDIA Driver. I was using version 378.49 and I rolled back to version 376.33. Now, the problem is solved. You can see more details on https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000060510-Process-finished-with-exit-code-1073740791-0xC0000409-JavaFX-Application

Thanks to @KrishnanMahadevan for reminding me to use IntelliJ IDE.