Reputation: 1055
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.
Upvotes: 5
Views: 7884
Reputation: 16060
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());
}
Upvotes: 8
Reputation: 168825
(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)
).Upvotes: 1
Reputation: 9914
Try to load the snake.html file like this:
ClassLoader.getSystemResource("/WebContent/snake.html");
Upvotes: 0