Damiii Damiii - 6 months ago 36
Java Question

Loading a local html file on jar into web engine

I'm having a problem in loading an HTML file to my application onto my web engine.

Currently, if I run my application throw IntelliJ IDE. The HTML page is being loaded correctly! But when I make the project and I run the application to my terminal. The HTML file isn't being loaded at all...

So I tested multiple choices and saw different post here on stackoverflow. But couldn't solve it yet at all...

My code for loading HTML page to my web engine.

WebEngine we = wv.getEngine();
we.setJavaScriptEnabled(true);
String htmlFile = getClass().getResource("/html/index.html").toExternalForm();
System.out.println(">>>"+htmlFile);
we.load(htmlFile);


Why do I have my println ? To check the path of my HTML page.
It seems that when I run it on my project IntelliJ. The application runs correctly. As shown under:

>>>file:/Developer/Liss-SDE/out/production/liss/html/index.html


But when I do it on my terminal. It prints me something which I don't understand...

>>>jar:file:/Users/damien/Desktop/liss%7cSDE/liss.jar!/html/index.html


Why does it put a jar: at the beginning? Also why is he putting a ! to my program .jar ? And if we compare to the other print, it is totally different!

I extracted the .jar file to check if the directory and the HTML file was missing, but everything is ok.
So what is the problem that I am having ?

I must say... I'm having a hard time with implementing files in Java or IntelliJ... This isn't so obivous for one thing who should be...

Answer

So I managed in solving this...

And I think this is a weird thing sincerely...

If I collapse all my code into the loading of the web engine everything works correctly...

sigh

I hope that future adventurers will not have this issue as I got...

Simply do that:

we.load(this.getClass().getResource("/html/index.html").toExternalForm());

I don't know if it is a bug or not... Because I didn't changed anything to my project unless putting this line of code...

Thank you to Paul Hicks for trying to help me :)