我正在尝试创建一种包装类来创建使用JavaFX WebView作为GUI的应用程序(我称之为类Application
),我可以在其他应用程序中轻松使用(我制作一个罐子我的包装器项目,然后jar可以在任何其他项目中导入)。我假设在我的包装器中包含一些调试功能很重要,所以我将'firebug-lite.js'和'firebug-lite.css'版本1.2下载到我的项目文件夹中的./src/firebug/目录(所以不需要互联网)。经过多次尝试后,我开始关注Application.debug()
中的代码:
public void debug() {
// file is inside the jar, in ./firebug/ directory, so I use getResourceAsStream()
BufferedReader br = new BufferedReader(new InputStreamReader(
getClass().getResourceAsStream("/firebug/firebug-lite.js")));
String line;
String wholeFirebugCode = "";
try {
// reading firebug-lite.js line-by-line, not including \n characters
while ((line = br.readLine()) != null) {
wholeFirebugCode += line;
}
if (wholeFirebugCode.isEmpty()) System.out.println("!");
else {
// everything is OK, file is read, so add firebug to our page
engine.executeScript("document.body.innerHTML += \""+
"<script type='text/javascript'>" +
wholeFirebugCode +
"</script>\"");
}
} catch (IOException e) {
printError(e, "during load of /firebug/firebug-lite.js from within jar (WebEngineApp.jar)");
}
}
所以,我构建了一个jar,检查它是否有./firebug/firebug-lite.js(确实如此),在示例项目中包含该jar并尝试使用此代码:
// constructor works fine
Application app = new Application("title of window", path_to_sample_html_page,
width, height, bridge);
app.run();
try {//we wait a few til page is fully loaded (just for sure)
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
printError(e, "");
}
// here I check if java-js connection is established and page is loaded. It is
Platform.runLater(app::test);
// here I run the code which cause problem
Platform.runLater(app::debug);
但这不起作用。我使用Java 8,如果它有任何区别:
Exception in thread "JavaFX Application Thread" netscape.javascript.JSException: SyntaxError: Unexpected identifier 'css'
at com.sun.webkit.dom.JSObject.fwkMakeException(JSObject.java:146)
at com.sun.webkit.WebPage.twkExecuteScript(Native Method)
at com.sun.webkit.WebPage.executeScript(WebPage.java:1509)
at javafx.scene.web.WebEngine.executeScript(WebEngine.java:1005)
at emeshka.webengineapp.Application.debug(Application.java:157)
at Main.lambda$main$0(Main.java:41)
(... etc)
文件似乎正确读取,我通过记录检查了它。如果我尝试使用'firebug-lite-compressed.js',如果我使用这种代码方法,包括:
engine.executeScript(wholeFirebugCode);
我完全迷失了。它不可能是萤火虫在语法上不正确。示例html页面不包含js。我怎样才能使萤火虫工作? 我试图使用这些需要互联网的解决方案,但它们没有起作用,并且没有显示任何错误: Html/Javascript debugging in JavaFX WebView , JAVAFX / WebView / WebEngine FireBugLite or Some other debugger?