2012-02-21 32 views
2

我遇到了在編輯器窗格中加載HTML文件並顯示它的問題。我使用的代碼是:從Eclipse文件夾加載Java 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 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) 

截圖:

enter image description here

任何人可以幫助嗎?

回答

3

這看起來像文件URL中的相對路徑。你需要使用絕對路徑。對於您的應用程序捆綁在一起的資源,你可以得到一個URL是這樣的:

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

這個假設有在你的classpath的根名爲「foobar.html」的HTML文件。擴展僞代碼以滿足您的需求。

+0

試過類似assets/www/index.html的東西,但那也是faild – Dallox 2012-02-21 16:48:51

+0

@Dallox - 這也是一個相對的URL。對於Unix,絕對路徑將以'/'或'/ home'(或Mac上的'/ Users')開始。對於Windows,它將以'C:\'的盤符開頭。 – Perception 2012-02-21 16:54:13

+0

但是html文件需要在jar中編譯(使用eclipse)那麼我怎樣才能給出絕對路徑 – Dallox 2012-02-21 17:21:24