让Java GUI打开网页浏览器

34

我想让Java GUI打开一个网页。因此,GUI运行一些代码并生成HTML文件。 然后,我希望在创建文件时立即在Web浏览器(最好是Firefox)中打开该文件。 我应该如何操作?

4个回答

39
如果您使用的是Java 6或更高版本,请查看Desktop API,特别是browse。使用方法如下(未经测试):
// 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);

嘿,还是无法让它正常工作。我收到了以下错误信息:“错误消息:系统找不到指定的路径。”尽管我已经提供了确切的路径。 - The Special One
@TheSpecialOne - 你是否提供了一个本地路径来指向文件?例如:在 Windows 系统上,htmlFilePath 应该是这样的 "c:\path\to\file.html"。不要使用 URL 语法... - Dan Vinton

27

如果你想通过Java程序在默认的浏览器中打开网页,可以尝试使用以下代码。

/// 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

3

我知道所有这些答案基本上都回答了这个问题,但是这里有一个优雅地失败的方法的代码。

请注意,该字符串可以是html文件的位置。

/**
* If possible this method opens the default browser to the specified web page.
* If not it notifies the user of webpage's url so that they may access it
* manually.
* 
* @param url
*            - this can be in the form of a web address (http://www.mywebsite.com)
*            or a path to an html file or SVG image file e.t.c 
*/
public static void openInBrowser(String url)
{
    try
        {
            URI uri = new URL(url).toURI();
            Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
            if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
                desktop.browse(uri);
            } else {
                throw new Exception("Desktop not supported, cannout open browser automatically");
            }
        }
    catch (Exception e)
        {
            /*
             *  I know this is bad practice 
             *  but we don't want to do anything clever for a specific error
             */
            e.printStackTrace();

            // Copy URL to the clipboard so the user can paste it into their browser
            StringSelection stringSelection = new StringSelection(url);
            Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
            clpbrd.setContents(stringSelection, null);
            // Notify the user of the failure
            WindowTools.informationWindow("This program just tried to open a webpage." + "\n"
                + "The URL has been copied to your clipboard, simply paste into your browser to access.",
                    "Webpage: " + url);
        }
}

0

我曾经成功地使用BrowserLauncher2。它可以在所有测试过的平台上调用默认浏览器。我用它来通过JNLP演示软件。该软件下载、运行并将用户的浏览器驱动到信息页面/反馈等。

我相信需要JDK 1.4或更高版本。


如果由于用户活动导致页面的URL发生更改,是否可以使用BrowserLauncher2获取新的URL? - Nikolay Kuznetsov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接