JAR文件中打开HTML文件

JAR文件中打开HTML文件

本文介绍了在.JAR文件中打开HTML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为snake.html的html文件,我想将其放入jar中。当jar运行时,主类应该在浏览器中打开这个html文件。我试过了:

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:

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());
}

如果我只是运行这段代码但是当我运行它时( html文件)我得到以下异常:

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

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)

我想知道我是否有类路径问题或者我没有正确地将jar指向文件。这个jar有两个目录,snake和WebContent。 Snake有snake.class文件,WebContent有snake.html。

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.

任何和所有的帮助/批评都赞赏。

Any and all help/criticism appreciated.

推荐答案

你必须首先解压缩文件。

You'll have to devompress the file first.

类似的东西:

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());
}

这篇关于在.JAR文件中打开HTML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 18:56