在.JAR文件中打开一个HTML文件

时间:2023-01-13 12:49:54

I have an html file called snake.html that I would like to put inside a jar. When the jar is run the main class should open this html file in the browser. I have tried:

我有一个名为snake的html文件。我想把html放到一个jar里面。当运行jar时,主类应该在浏览器中打开这个html文件。我有尝试:

public static void main(String[] args) throws IOException, URISyntaxException {
    URL url = Snake.class.getResource("/WebContent/snake.html");
    System.out.println(url);
    // relative to the class location
    Desktop.getDesktop().browse(url.toURI());
}

Which works if I just run this code but when I jar it (and the html file) I get the following exception:

如果我只运行这段代码,它就可以工作,但是当我将它(和html文件)jar文件时,我得到以下异常:

Exception in thread "main" java.io.IOException: Failed to mail or browse
       jar:file:/Users/~user~/Desktop/Snake%20v0.1.jar!/WebContent/snake.html. 
       Error code: -10814
at apple.awt.CDesktopPeer.lsOpen(CDesktopPeer.java:52)
at apple.awt.CDesktopPeer.browse(CDesktopPeer.java:45)
at java.awt.Desktop.browse(Desktop.java:368)
at snake.Snake.main(Snake.java:26)

Im wondering if I have a classpath issue or maybe Im not directing the jar to the file correctly. THe jar has two directories, snake and WebContent. Snake has the snake.class file and WebContent has snake.html.

我想知道我是否有类路径问题,或者我没有正确地将jar定向到文件。jar有两个目录,snake和WebContent。蛇有蛇。类文件和WebContent具有snac .html。

Any and all help/criticism appreciated.

任何帮助/批评都是值得赞赏的。

3 个解决方案

#1


6  

You'll have to devompress the file first.

你得先按下文件。

Something like:

喜欢的东西:

public static void main(String[] args) throws IOException, URISyntaxException {
    URL url = Snake.class.getResource("/WebContent/snake.html");


    File temp = File.createTempfile();
    temp.deleteOnExit();

    // Copy content 

    Desktop.getDesktop().browse(temp.getAbsolutePath());
}

#2


1  

(HTML) ..inside a jar. When the jar is run the main class should open this html file in the browser.

(HTML). .在一个罐子里。当运行jar时,主类应该在浏览器中打开这个html文件。

Browsers are not designed to display HTML inside Java archives. Java components like JEditorPane can. If the HTML renders to your satisfaction inside a Swing component, use that. Otherwise it will be necessary to

浏览器并不是为了在Java文档中显示HTML而设计的。像JEditorPane这样的Java组件可以。如果HTML在Swing组件中呈现您的满意,请使用它。否则就有必要

  1. Locate the resource by URL.
  2. 通过URL定位资源。
  3. Extract it to a location on the local file-system.
  4. 将它解压到本地文件系统的某个位置。
  5. Use the browser to open the file (the easiest way is using Desktop.open(File)).
  6. 使用浏览器打开文件(最简单的方法是使用Desktop.open(file))。

#3


0  

Try to load the snake.html file like this:

试着给蛇上膛。html文件是这样的:

ClassLoader.getSystemResource("/WebContent/snake.html");

#1


6  

You'll have to devompress the file first.

你得先按下文件。

Something like:

喜欢的东西:

public static void main(String[] args) throws IOException, URISyntaxException {
    URL url = Snake.class.getResource("/WebContent/snake.html");


    File temp = File.createTempfile();
    temp.deleteOnExit();

    // Copy content 

    Desktop.getDesktop().browse(temp.getAbsolutePath());
}

#2


1  

(HTML) ..inside a jar. When the jar is run the main class should open this html file in the browser.

(HTML). .在一个罐子里。当运行jar时,主类应该在浏览器中打开这个html文件。

Browsers are not designed to display HTML inside Java archives. Java components like JEditorPane can. If the HTML renders to your satisfaction inside a Swing component, use that. Otherwise it will be necessary to

浏览器并不是为了在Java文档中显示HTML而设计的。像JEditorPane这样的Java组件可以。如果HTML在Swing组件中呈现您的满意,请使用它。否则就有必要

  1. Locate the resource by URL.
  2. 通过URL定位资源。
  3. Extract it to a location on the local file-system.
  4. 将它解压到本地文件系统的某个位置。
  5. Use the browser to open the file (the easiest way is using Desktop.open(File)).
  6. 使用浏览器打开文件(最简单的方法是使用Desktop.open(file))。

#3


0  

Try to load the snake.html file like this:

试着给蛇上膛。html文件是这样的:

ClassLoader.getSystemResource("/WebContent/snake.html");