我知道這個問題已經發布了,但我對它進行了徹底的研究,但我無法讓它發揮作用。所以這是我的配置、代碼和我得到的運行時例外。簡短地說,這是我得到的例外:無法訪問類 com.sun.javafx.webkit.WebConsoleListener(在模塊 javafx.web 中),因為模塊 javafx.web 不匯出 com.sun.javafx.webkit
更詳盡:
IntelliJ Idea 使用 Maven Java openjdk-17 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;
}
Java代碼:
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();
}
}
使用 IntelliJ 運行:(注意引數:-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
問題:我怎樣才能擺脫這個例外并查看日志訊息
uj5u.com熱心網友回復:
我在此設定的回答中添加了一些進一步的解釋:
- 在 IntelliJ 中,--覆寫每個模塊的編譯器引數是什么意思?
答案中還有很多背景關系資訊:
- javafx webview 無法正確顯示 html 頁面
- JavaFX 12 中的 WebConsoleListener IllegalAccessError
所以你的配置有很多問題。這些是我可以確定的,希望沒有更多(我認為我無法幫助您解決所有環境問題):
- 您指定
--add-exports為程式的引數而不是 VM 選項。
- 要了解這一點以及如何修復它,請參閱:如何在 IntelliJ IDEA 中設定 JVM 引數?
- 您的代碼位于命名模塊中,而不是未命名模塊中:
- 您需要在命令列中匯出到
com.ittopics.message_log_demo,而不是ALL-UNNAMED
- 您有一個
module-info.java已經需要 JavaFX 模塊的檔案,并且您的應用程式運行時帶有-p配置模塊路徑的選項(查看 java 命令列實際使用的想法),因此它可以找到這些模塊(由 maven 下載,而不是 JavaFX SDK 下載),但是,您還嘗試將來自 SDK 下載的其他模塊添加到模塊路徑中。
- 你不需要
"--module-pathc:\Users\User\javafx-sdk-17.0.0.1\lib" - 即使你有它,之后也需要一個空格
--module-path - 我不知道 Windows 上引數的參考規則,所以我不確定在這種情況下是否需要(或會破壞某些東西)。
- 您已經在
module-info.java檔案中需要所需的模塊。
- 您也不需要
--add-modules javafx.web,javafx.fxml,javafx.base在命令列中。
- 你的
add-exports論點是錯誤的。
您的問題說它使用
-add-exports,盡管這可能是您在提出問題時犯的復制粘貼錯誤。--add-exports是必需的(注意-前綴中有兩個字符)。
你有一個
=之后add-exports,不應該。它應該是:--add-exports javafx.web/com.sun.javafx.webkit=com.ittopics.message_log_demo不要使用 JavaFX 的早期訪問版本,將依賴項中使用的版本從
17-ea 11更改為17.0.0.1或其他一些穩定版本。發布代碼時,請盡量使其最小化(包括依賴項和模塊描述符資訊)。
- 嘗試對匯入 10 個不同庫的問題進行故障排除要困難得多,而后者只匯入該測驗所需的單個直接依賴庫(
javafx.web模塊,因為任何其他需要的模塊將被傳遞匯入)。
請多加小心。
即使在您修復了設定之后:
我又跑的例子中,使用你原來的參考的問題所提供的網址:
http://it-topics.com/index3.html。它不再為 ResizeObserver 提供錯誤,所以我猜有人修復了網站,至少現在是這樣。
因此,即使您正確注冊了 WebConsoleListener 并進行記錄,它也不會再記錄該頁面的錯誤。
注意 JavaFX 本身仍然不處理 ResizeObserver,因此如果您使用 Mozilla 測驗頁面進行 ResizeObserver,它將報告錯誤
https://mdn.github.io/dom-examples/resize-observer/resize-observer-text.html。和以前一樣,如果您希望這樣做,我鼓勵您花時間跟進并提交 ResizeObserver 對 WebView 支持的功能請求。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/329690.html
上一篇:Maven依賴項限制為私有存盤庫
