从Eclipse文件夹加载Java HTML文件
在编辑器窗格中加载HTML文件并显示它时遇到问题。我使用的代码是:从Eclipse文件夹加载Java HTML文件,java,swing,url,jeditorpane,fileinputstream,Java,Swing,Url,Jeditorpane,Fileinputstream,在编辑器窗格中加载HTML文件并显示它时遇到问题。我使用的代码是: window_pane = new JEditorPane("file:///assets/www/index.html"); 但这只是给出了一些错误: Exception in thread "main" java.io.FileNotFoundException: \assets\www\index.html (Het systeem kan het opgegeven pad niet vinden) at ja
window_pane = new JEditorPane("file:///assets/www/index.html");
但这只是给出了一些错误:
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)
截图:
有人能帮忙吗?这看起来像是文件URL中的相对路径。您需要使用绝对路径。对于与应用程序捆绑在一起的资源,您可以获得如下URL:
final String resourcePath = "foobar.html";
URL resourceURL = Thread.currentThread().getContextClassLoader().getResource(resourcePath);
JEditorPane editorPane = new JEditorPane(resourceURL);
这假设在类路径的根目录下有一个名为“foobar.HTML”的HTML文件。扩展伪代码以满足您的需要。这看起来像是文件URL中的相对路径。您需要使用绝对路径。对于与应用程序捆绑在一起的资源,您可以获得如下URL:
final String resourcePath = "foobar.html";
URL resourceURL = Thread.currentThread().getContextClassLoader().getResource(resourcePath);
JEditorPane editorPane = new JEditorPane(resourceURL);
这假设在类路径的根目录下有一个名为“foobar.HTML”的HTML文件。扩展伪代码以满足您的需要。尝试了资产/www/index.html之类的方法,但失败了too@Dallox-这也是一个相对URL。对于Unix,绝对路径将以Mac上的/or/home或/Users开始。对于Windows,它将以一个像'C:`这样的驱动器号开始。但是html文件需要使用eclipse在jar中编译,所以我如何才能给出一个绝对路径there@Dallox-你在最初的问题中没有提到这一点。请参阅对我答案的编辑。感谢代码在我将index.html放在src文件夹中时起作用,我想我会将我的html文件保存在那里,非常感谢:d编辑了诸如assets/www/index.html之类的内容,但失败了too@Dallox-这也是一个相对URL。对于Unix,绝对路径将以Mac上的/or/home或/Users开始。对于Windows,它将以一个像'C:`这样的驱动器号开始。但是html文件需要使用eclipse在jar中编译,所以我如何才能给出一个绝对路径there@Dallox-你在最初的问题中没有提到这一点。请参阅对我答案的编辑。感谢代码在我将index.html放入src文件夹时起作用,我想我会将我的html文件保存在那里,非常感谢:D