Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open an HTML File Inside a .JAR File

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

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)

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.

like image 948
shortspider Avatar asked Apr 17 '12 05:04

shortspider


People also ask

How do I view inside a JAR file?

JAR files are packaged in the ZIP file format. The unzip command is a commonly used utility for working with ZIP files from the Linux command-line. Thanks to the unzip command, we can view the content of a JAR file without the JDK.

Can .jar files be opened?

How To Open JAR Files. If you want to view each file in a non-executable jar file, you can do that with the help of a JAR file compatible compression/decompression software. Either WinRAR or 7-ZIP, any one of them is a good choice. After you have installed WinRAR or 7-ZIP, run it, open the file, and extract files in it ...

Can a Web application be executed from a .JAR file?

You could run the chatserver. jar on the server, and put chatclient. jar inside an applet, in order for the users to be able to run it inside a browser.


2 Answers

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());
}
like image 75
Christian Kuetbach Avatar answered Oct 24 '22 15:10

Christian Kuetbach


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

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

  1. Locate the resource by URL.
  2. Extract it to a location on the local file-system.
  3. Use the browser to open the file (the easiest way is using Desktop.open(File)).
like image 29
Andrew Thompson Avatar answered Oct 24 '22 15:10

Andrew Thompson