2
votes

I want to try out the feature of webview in my javafx project, but I kept having java.lang.reflect.InvocationTargetException, so I even tried out the sample code for WebView from jenkov.com, I still couldn't run the application. Is there anything wrong with my configurations or is it something else?

package sample;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    public void start(Stage primaryStage) {
        primaryStage.setTitle("JavaFX WebView Example");

        WebView webView = new WebView();

        webView.getEngine().load("http://google.com");

        VBox vBox = new VBox(webView);
        Scene scene = new Scene(vBox, 960, 600);

        primaryStage.setScene(scene);
        primaryStage.show();

    }
}

This is the result I got:

"C:\Program Files\AdoptOpenJDK\jdk-11.0.4.11-openj9\bin\java.exe" --module-path "C:/Program Files/AdoptOpenJDK/javafx-sdk-11.0.2/lib" --add-modules javafx.controls,javafx.fxml --add-modules javafx.base,javafx.graphics --add-reads javafx.base=ALL-UNNAMED --add-reads javafx.graphics=ALL-UNNAMED "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.1.3\lib\idea_rt.jar=51495:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.1.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Users\User\Desktop\Test\out\production\Test;C:\Program Files\AdoptOpenJDK\javafx-sdk-11.0.2\lib\javafx-swt.jar;C:\Program Files\AdoptOpenJDK\javafx-sdk-11.0.2\lib\javafx.base.jar;C:\Program Files\AdoptOpenJDK\javafx-sdk-11.0.2\lib\javafx.controls.jar;C:\Program Files\AdoptOpenJDK\javafx-sdk-11.0.2\lib\javafx.fxml.jar;C:\Program Files\AdoptOpenJDK\javafx-sdk-11.0.2\lib\javafx.graphics.jar;C:\Program Files\AdoptOpenJDK\javafx-sdk-11.0.2\lib\javafx.media.jar;C:\Program Files\AdoptOpenJDK\javafx-sdk-11.0.2\lib\javafx.swing.jar;C:\Program Files\AdoptOpenJDK\javafx-sdk-11.0.2\lib\javafx.web.jar" sample.Main

Exception in Application start method
java.lang.reflect.InvocationTargetException
    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)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    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)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at com.sun.javafx.application.LauncherImpl$$Lambda$110.000000005B9305B0.run(Unknown Source)
    at java.base/java.lang.Thread.run(Thread.java:831)
Caused by: java.lang.IllegalAccessError: com/sun/javafx/sg/prism/NGGroup is not visible
    at java.base/java.lang.ClassLoader.defineClassImpl(Native Method)
    at java.base/java.lang.ClassLoader.defineClassInternal(ClassLoader.java:465)
    at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:434)
    at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
    at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:1114)
    at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:902)
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:808)
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:766)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:1067)
    at sample.Main.start(Main.java:20)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at com.sun.javafx.application.LauncherImpl$$Lambda$113.000000005BA1B2E0.run(Unknown Source)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at com.sun.javafx.application.PlatformImpl$$Lambda$106.000000005B73A6D0.run(Unknown Source)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at com.sun.javafx.application.PlatformImpl$$Lambda$108.000000005B92FE10.run(Unknown Source)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:704)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at com.sun.javafx.application.PlatformImpl$$Lambda$107.000000005B92F5B0.run(Unknown Source)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at com.sun.glass.ui.win.WinApplication$$Lambda$96.000000005B3D8280.run(Unknown Source)
    ... 1 more
Exception running application sample.Main
1
You have to add the module javafx.web to —add-modules, so far it is only included in the classpath.José Pereda
@JoséPereda Problem solved! Really appreciated!bonus

1 Answers

1
votes

Yes, José Pereda's comment above seems to be true! I've had the same problem and after adding javafx.web to my VM-arguments all worked fine!

This should be marked as an answer to the question above, in order for others to find it easier!

--module-path "\path\to\your\javafx-sdk\lib" --add-modules=javafx.controls,javafx.web