Reputation: 627
how to I open a URL with the systems standard browser with Java?
I currently use this code for opening a specific URL (locally stored html file), which works fine when I run the application with my IDE (Eclipse), but after bundling the software, it doesn't work any more.
url = MainWindow.class.getResource("mySite.html");
helpMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
java.awt.Desktop.getDesktop().browse(url.toURI());
}
catch (URISyntaxException e1) {
e1.printStackTrace();
}
catch (IOException e1) {
e1.printStackTrace();
}
}
});
Any suggestsions? Thank you very much!
Upvotes: 2
Views: 927
Reputation: 35351
Bare Bones Browser Launch is a good solution. It's very easy to use:
BareBonesBrowserLaunch.openURL("http://www.stackoverflow.com");
Upvotes: 0
Reputation: 1109665
but after bundling the software, it doesn't work any more.
You cannot browse to URL's which points to resources inside a JAR file. You need to extract the resource (just get InputStream
using getResourceAsStream()
) and store it somewhere else (as temp file?) and then browse it instead.
Upvotes: 2