Home > Blockchain >  How to get the JavaFx WebEngine to report errors in details?
How to get the JavaFx WebEngine to report errors in details?

Time:10-21

I know this question has already been posted but I had a thorough look into it, and I could not make it work. So here is my config, the code and the runtime exception I get. To be short here is the exception I get : cannot access class com.sun.javafx.webkit.WebConsoleListener (in module javafx.web) because module javafx.web does not export com.sun.javafx.webkit

To be more exhaustive :

IntelliJ Idea using Maven Java openjdk-17 the module-info.java

module com.ittopics.message_log_demo {
requires javafx.controls;
requires javafx.fxml;

requires org.controlsfx.controls;
requires com.dlsc.formsfx;
requires validatorfx;
requires javafx.web;

opens com.ittopics.message_log_demo to javafx.fxml, javafx.web;
exports com.ittopics.message_log_demo;
}

The java code :

package com.ittopics.message_log_demo;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebEvent;
import javafx.scene.web.WebView;
import javafx.stage.Stage;


import com.sun.javafx.webkit.WebConsoleListener;

public class Main extends Application {


@Override
public void start(final Stage pStage) {
       initStage(pStage);
}

private void initStage(Stage pStage){

    WebView lWebView = new WebView();
    WebEngine lWebViewEngine = lWebView.getEngine();

    lWebViewEngine.getLoadWorker().exceptionProperty().addListener((ov, t, t1) ->
            System.out.println("Received exception: " t1.getMessage())
    );

    WebConsoleListener.setDefaultListener((webViewReference, message, lineNumber, sourceId) ->
            System.out.println("Console: ["   sourceId   ":"   lineNumber   "] "   message)
    );
   lWebViewEngine.getLoadWorker().exceptionProperty().addListener(new ChangeListener<Throwable>() {
        @Override
        public void changed(ObservableValue<? extends Throwable> ov, Throwable t, Throwable t1) {
            System.out.println("Received exception: " t1.getMessage());
        }
    });

 


    lWebViewEngine.setOnAlert((EventHandler<WebEvent<String>>) event -> System.out.println(event.getData()));
    
    String lTargetUrl;
    
    lTargetUrl = "file:///C:/Users/User/Documents/index3.html";
    lWebViewEngine.load(lTargetUrl);

    VBox lVBox = new VBox(lWebView);

    pStage.setScene(new Scene(lVBox));
    
    pStage.show();
}


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

Run with IntelliJ : (note the parameters : -add-exports=javafx.web/com.sun.javafx.webkit=ALL-UNNAMED "--module-pathc:\Users\User\javafx-sdk-17.0.0.1\lib" --add-modules javafx.web,javafx.fxml,javafx.base)

C:\Users\User\.jdks\openjdk-17\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.2.2\lib\idea_rt.jar=64692:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.2.2\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\User\.m2\repository\org\openjfx\javafx-controls\17-ea 11\javafx-controls-17-ea 11.jar;C:\Users\User\.m2\repository\org\openjfx\javafx-graphics\17-ea 11\javafx-graphics-17-ea 11.jar;C:\Users\User\.m2\repository\org\openjfx\javafx-base\17-ea 11\javafx-base-17-ea 11.jar;C:\Users\User\.m2\repository\org\openjfx\javafx-fxml\17-ea 11\javafx-fxml-17-ea 11.jar;C:\Users\User\.m2\repository\org\openjfx\javafx-web\17-ea 11\javafx-web-17-ea 11.jar;C:\Users\User\.m2\repository\org\openjfx\javafx-media\17-ea 11\javafx-media-17-ea 11.jar -p C:\Users\User\.m2\repository\org\controlsfx\controlsfx\11.1.0\controlsfx-11.1.0.jar;C:\Users\User\.m2\repository\org\openjfx\javafx-media\17-ea 11\javafx-media-17-ea 11-win.jar;C:\Users\User\.m2\repository\org\openjfx\javafx-web\17-ea 11\javafx-web-17-ea 11-win.jar;C:\Users\User\.m2\repository\org\openjfx\javafx-graphics\17-ea 11\javafx-graphics-17-ea 11-win.jar;C:\Users\User\.m2\repository\com\dlsc\formsfx\formsfx-core\11.3.2\formsfx-core-11.3.2.jar;C:\Users\User\.m2\repository\org\openjfx\javafx-controls\17-ea 11\javafx-controls-17-ea 11-win.jar;C:\Users\User\.m2\repository\org\openjfx\javafx-fxml\17-ea 11\javafx-fxml-17-ea 11-win.jar;C:\Users\User\IdeaProjects\message_log_demo\target\classes;C:\Users\User\.m2\repository\net\synedra\validatorfx\0.1.13\validatorfx-0.1.13.jar;C:\Users\User\.m2\repository\org\openjfx\javafx-base\17-ea 11\javafx-base-17-ea 11-win.jar -m com.ittopics.message_log_demo/com.ittopics.message_log_demo.Main --add-exports=javafx.web/com.sun.javafx.webkit=ALL-UNNAMED "--module-pathc:\Users\User\javafx-sdk-17.0.0.1\lib\" --add-modules javafx.web,javafx.fxml,javafx.base"
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:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:467)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:366)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:903)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
    at java.base/java.lang.Thread.run(Thread.java:833)
**Caused by: java.lang.IllegalAccessError: class com.ittopics.message_log_demo.Main (in module com.ittopics.message_log_demo) cannot access class com.sun.javafx.webkit.WebConsoleListener (in module javafx.web) because module javafx.web does not export com.sun.javafx.webkit to module com.ittopics.message_log_demo**
    at com.ittopics.message_log_demo/com.ittopics.message_log_demo.Main.initStage(Main.java:35)
    at com.ittopics.message_log_demo/com.ittopics.message_log_demo.Main.start(Main.java:23)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:849)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:474)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:447)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:446)
    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)
    ... 1 more
Exception running application com.ittopics.message_log_demo.Main

Process finished with exit code 1

Question : how can I get rid of this exception and see the log messages

CodePudding user response:

I added some further explanation on this setup, in the answer to:

There is also a lot of contextual information in the answers to:


So you had a lot of problems with your configuration. These were what I could determine, hopefully there are no more (I don't think I can help you with all of your environmental issues):

  1. You are specifying --add-exports as an argument to your program instead of a VM Option.
  1. Your code is in a named module, not an unnamed module:
  • You need to export to com.ittopics.message_log_demo, not ALL-UNNAMED, in your command line
  1. You have a module-info.java file that requires the JavaFX modules already and your app is run with the -p option to configure the module path (see what idea actually used for the java command line), so it can find those modules (downloaded by maven, not the JavaFX SDK download), which you have, BUT, you also try to add to the module path, additional modules from an SDK download.
  • You don't need "--module-pathc:\Users\User\javafx-sdk-17.0.0.1\lib"
  • Even if you did have it, a space is required after --module-path
  • I don't know about quotation rules for arguments on Windows, so I am not sure if they are needed (or would break something) in this case.
  1. You already require the modules you need in your module-info.java file.
  • You don't need --add-modules javafx.web,javafx.fxml,javafx.base also in your command line.
  1. Your add-exports argument is wrong.
  • Your question says it uses -add-exports, though that may have been a copy-paste error you made when asking the question.

  • --add-exports is required (note two - characters in the prefix).

  1. you have an = after add-exports and should not. It should be:

    --add-exports javafx.web/com.sun.javafx.webkit=com.ittopics.message_log_demo
    
  2. Don't use early access releases of JavaFX, change the version used in your dependencies from 17-ea 11 to 17.0.0.1 or some other stable version.

  3. When posting code, please try to make it minimal (including the dependency and module descriptor info).

  • It is much harder to try troubleshoot something that imports ten different libraries then one which only imports the single required directly dependent library for this test (the javafx.web module, as any others needed will be imported transitively).

Please be more careful.


Even after you have fixed your setup:

I ran the example again, using the url provided in your original referenced question: http://it-topics.com/index3.html.

It no longer gives an error for the ResizeObserver, so I guess somebody fixed the website, at least for now.

So, even once you get a WebConsoleListener properly registered and logging it won't log an error for that page anymore.

Note JavaFX itself still doesn't handle the ResizeObserver, so if you use the mozilla test page for ResizeObserver, it will report an error https://mdn.github.io/dom-examples/resize-observer/resize-observer-text.html.

As before, I encourage you to spend the time to follow-up and file a feature request for ResizeObserver support for WebView if that is something you wish to do.

  • Related