Dallox
Dallox

Reputation: 487

Java HTML file load from Eclipse folder

I am encountering a problem loading an HTML file in an editor pane and displaying it. The code I am using is:

window_pane = new JEditorPane("file:///assets/www/index.html");

But that just gave some errors:

Exception in thread "main" java.io.FileNotFoundException: \assets\www\index.html (Het systeem kan het opgegeven pad niet vinden)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at sun.net.www.protocol.file.FileURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.file.FileURLConnection.getInputStream(Unknown Source)
    at javax.swing.JEditorPane.getStream(Unknown Source)
    at javax.swing.JEditorPane.setPage(Unknown Source)
    at javax.swing.JEditorPane.setPage(Unknown Source)
    at javax.swing.JEditorPane.<init>(Unknown Source)
    at nl.xedus.battlex.java.WebBrowser.<init>(WebBrowser.java:33)
    at nl.xedus.battlex.java.WebBrowser.main(WebBrowser.java:72)

ScreenShot:

enter image description here

Can anybody help please?

Upvotes: 2

Views: 1805

Answers (1)

Perception
Perception

Reputation: 80623

That looks like a relative path in your file URL. You need to use an absolute path. For resources bundled with your application you can get a URL like this:

final String resourcePath = "foobar.html";
URL resourceURL = Thread.currentThread().getContextClassLoader().getResource(resourcePath);
JEditorPane editorPane = new JEditorPane(resourceURL);

This assumes that there is an HTML file named 'foobar.html' at the root of your classpath. Extend the pseudocode to serve your needs.

Upvotes: 4

Related Questions