请帮助!
我的目标是将本地Web文件打包以在JAR文件中分发。 我将本地html文件存储在项目的根目录中。另外,我将该文件夹添加到了Projct Source包中。现在在我的程序中。我用
File f = new File("folder\\index.html");
engine.load(f.toURI().toString());
该应用程序运行正常,但是当分配到另一个系统时,该应用程序将不会打开网页,而是显示空白窗口。这意味着本地文件未与应用程序一起编译。
此外,我已经尝试过:
URL url = this.getClass().getClassLoader().getResource("folder\\index.html");
engine.load(url.toString());
我已经尝试过了:
engine.load(getClass().getResource("Cinnamon\\index.html").toExternalForm());
编译时会出现错误:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at cinnamonapp.CinnamonApp.start(CinnamonApp.java:44)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
... 1 more
Exception running application cinnamonapp.CinnamonApp
Picked up _JAVA_OPTIONS: -Xmx512M
Java Result: 1
我在做什么错或没有做?请帮忙!!!!
答案 0 :(得分:0)
我终于明白了。我做错了是因为我不了解相对路径和绝对路径问题。认识到engine.load
之外的url
。我所做的就是将我的资源文件夹(放置html文件的文件夹)与我的Class放在同一目录中,然后使用以下代码
String url = getClass().getResource("Cinnamon/index.html").toExternalForm();
engine.load(url);
总结:
如果资源与类位于同一目录,则不需要(/)
使用开始的斜杠(“ /”)表示到项目根目录的相对路径。
使用开头的点斜杠(“ ./”)表示指向类路径的相对路径
要进一步阅读@DVarga的支票,请点击此处JavaFX resource handling: Load HTML files in WebView