使用java gui在web浏览器中打开网页

使用java gui在web浏览器中打开网页,java,html,user-interface,Java,Html,User Interface,我正在尝试使用JavaGUI打开网页。因此gui运行一些代码来做一些事情,然后生成一个html文件。 然后,我希望该文件一创建就在web浏览器(首选Firefox)中打开。 我该怎么做呢?如果您使用的是Java 6或更高版本,请特别参阅API。这样使用(未经测试): 我成功地使用了它。它将在所有测试平台上调用默认浏览器。我使用它通过JNLP演示软件。软件下载、运行并驱动用户的浏览器浏览信息页面/反馈等 我相信JDK 1.4及以上版本。是的,但是如果您想通过java程序在默认web浏览器中打开网页

我正在尝试使用JavaGUI打开网页。因此gui运行一些代码来做一些事情,然后生成一个html文件。 然后,我希望该文件一创建就在web浏览器(首选Firefox)中打开。
我该怎么做呢?

如果您使用的是Java 6或更高版本,请特别参阅API。这样使用(未经测试):

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


我相信JDK 1.4及以上版本。

是的,但是如果您想通过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

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

请注意,字符串可以是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);
        }
}

嘿还是无法让它工作。我收到以下错误:“错误消息:系统找不到指定的路径。”这是因为我已经给了它确切的路径。@TheSpecialOne-是否为文件提供了本地路径?例如:在windows框中,htmlFilePath看起来像“c:\path\to\file.html”。不要使用URL语法…如果页面的URL由于用户活动而更改,可以使用BrowserLauncher2获取新的URL吗?
/**
* 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);
        }
}