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.
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.
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 ...
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.
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());
}
(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
Desktop.open(File)
).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With