Open Browser window from Java program

拈花ヽ惹草 提交于 2019-11-30 21:16:54
anjanb

after having read the various answers and various comments(from questioner), here's what I would do

1) try this java approach http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ProcessBuilder.html

ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");
Map<String, String> env = pb.environment();
env.put("VAR1", "myValue");
env.remove("OTHERVAR");
env.put("VAR2", env.get("VAR1") + "suffix");
pb.directory("myDir");
Process p = pb.start();

see more about this class:

http://java.sun.com/developer/JDCTechTips/2005/tt0727.html#2
http://www.javabeat.net/tips/8-using-the-new-process-builder-class.html

2) try doing this(launching firefox) from C/C++/ruby/python and see if that is succeeding.

3) if all else fails, I would launch a shell program and that shell program would launch firefox!!

If you can narrow it down to Java 6, you can use the desktop API:

http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/desktop_api/

Should look something like:

    if (Desktop.isDesktopSupported()) {
        Desktop desktop = Desktop.getDesktop();
        if (desktop.isSupported(Desktop.Action.BROWSE)) {
            try {
                desktop.browse(new URI("http://localhost"));
            }
            catch(IOException ioe) {
                ioe.printStackTrace();
            }
            catch(URISyntaxException use) {
                use.printStackTrace();
            }
        }
    }

Use BrowserLauncher.

Invoking it is very easy, just go

new BrowserLauncher().openURLinBrowser("http://www.google.com");

You might have better luck if you read and display the standard output/error streams, so you can catch any error message firefox may print.

kta
try {
     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());
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!