2

I recently downloaded the latest JavaFX SDK 12 and I wish to intercept Console Messages in my JavaFX WebView.

So, I have this

WebConsoleListener.setDefaultListener((webView, message, lineNumber, sourceId) -> {
       //////// I am listening for a specific console message here in my 
      ///webview
  });

but I keep getting


Caused by: java.lang.IllegalAccessError: class rumbler.launcher.ApplicationLoader (in unnamed module @0x5c4c6905) cannot access class com.sun.javafx.webkit.WebConsoleListener (in module javafx.web) because module javafx.web does not export com.sun.javafx.webkit to unnamed module @0x5c4c6905

Here is my build.gradle file

javafx {
    version = "12.0.1"
    modules = ['javafx.base', 'javafx.controls', 'javafx.web']
}

Here are my VM OPTIONS

--module-path "path_to_\javafx-sdk-11.0.2\lib" --add-modules javafx.controls,javafx.fxml,javafx.web,javafx.base

.Am I missing something?

mekings
  • 361
  • 3
  • 17

1 Answers1

6

You are using private API, which is not advised.

Anyway, the error message is quite clear:

module javafx.web does not export com.sun.javafx.webkit to unnamed module @0x5c4c6905

Whenever you want to access some non-exposed package from your project (either modular on non-modular), you need to use --add-exports:

The command line option --add-exports $module/$package=$readingmodule exports $package of $module to $readingmodule. Code in $readingmodule can hence access all public types in $package but other modules can not. [source].

So in this case, the solution is straight forward:

--add-exports javafx.web/com.sun.javafx.webkit=ALL-UNNAMED \
--module-path "path_to_\javafx-sdk-11.0.2\lib" \
--add-modules javafx.web,javafx.fxml
José Pereda
  • 44,311
  • 7
  • 104
  • 132
  • Note: when adding exports at the command line: if your application provides a `module-info.java` file, then use the name of your application module from the `module-info.java` file, instead of `ALL-UNNAMED`. For more info, see the the related question: [How to get the JavaFx WebEngine to report errors in details?](https://stackoverflow.com/questions/69637166/how-to-get-the-javafx-webengine-to-report-errors-in-details/69645513#69645513). – jewelsea Oct 20 '21 at 12:07
  • To address "You are using private API, which is not advised." I want to mention related question "[What is the public API for getting JavaFX WebView console events?](https://stackoverflow.com/questions/55072824)", which I believe is the better canonical question/answer. (TL;DR: no code API available in 2022, but logging framework can be configured to emit same messages) – Jules Kerssemakers Sep 22 '22 at 11:37