我试图让java gui打开一个网页.因此,gui运行一些代码来执行操作,然后生成一个html文件.然后我希望这个文件在创建后立即在Web浏览器(最好是Firefox)中打开.我该怎么做呢?
如果您使用的是Java 6或更高版本,请参阅Desktop API,特别是浏览.像这样使用它(未经测试):
// using this in real life, you'd probably want to check that the desktop // methods are supported using isDesktopSupported()... String htmlFilePath = "path/to/html/file.html"; // path to your new file File htmlFile = new File(htmlFilePath); // open the default web browser for the HTML page Desktop.getDesktop().browse(htmlFile.toURI()); // if a web browser is the default HTML handler, this might work too Desktop.getDesktop().open(htmlFile);
Ya,但如果您想通过java程序在默认Web浏览器中打开网页,那么您可以尝试使用此代码.
/// file OpenPageInDefaultBrowser.java public class OpenPageInDefaultBrowser { public static void main(String[] args) { try { //Set your page url in this string. For eg, I m using URL for Google Search engine String url = "http://www.google.com"; java.awt.Desktop.getDesktop().browse(java.net.URI.create(url)); } catch (java.io.IOException e) { System.out.println(e.getMessage()); } } } /// End of file