How to launch standard browser out of Java application?
how to I open a URL with the systems standard browser with Java?
I currently u开发者_开发百科se 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!
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.
Any idea why it doesn't work? You can try JDIC, it has not been updated for a while but should do the trick.
Bare Bones Browser Launch is a good solution. It's very easy to use:
BareBonesBrowserLaunch.openURL("http://www.stackoverflow.com");
精彩评论