0

Please help!

My aim is to package a local web files for distribution in JAR file. I stored the local html files in the root directory of my project. Also, I added the folder to the Projct Source package. Now in my program. I used

 File f = new File("folder\\index.html");

 engine.load(f.toURI().toString());

The application run fine but when ditribute to another system the application will not open the web page rather it will display empty window. Meaning that the local file is not compiled with the application.

Also, I have tried this:

URL url = this.getClass().getClassLoader().getResource("folder\\index.html");

 engine.load(url.toString());

I have tried this:

engine.load(getClass().getResource("Cinnamon\\index.html").toExternalForm());

It gives error during compilation:

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

What am i doing wrong or have not done? Please help!!!!

Yemmy1000
  • 31
  • 2

1 Answers1

0

I finally got it. What I was doing wrong was that I failed to understand the relative vs absolute path issue. Realising that engine.load except just url. All I did was to put my resource folder (where the html files are placed) inside the same directory with my Class then use the following code

String url = getClass().getResource("Cinnamon/index.html").toExternalForm();
        engine.load(url);

In summary:

If the resource is the same directory as the class, no need for (/)

Using beginning slash("/") means relative path to the project root.

Using a beginning dot-slash("./") means relative path to path of the class

For further reading check contribution by @DVarga from here JavaFX resource handling: Load HTML files in WebView

Yemmy1000
  • 31
  • 2