tags:

views:

1203

answers:

5

I have a static method used to launch a browser with a given URL. When the browser is already open, this takes over the active browser window.

This is a problem if the browser is in use for something else, such as data entry. Is there a way to open a URL in a new Browser window (or tab)?

public static void openURL(String urlText)
{
    if (Desktop.isDesktopSupported())
    {
        URI uri = URI.create(urlText);
        try
        {
            Desktop.getDesktop().browse(uri);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

Alternately, is there a better way to do this?

A: 

Under Windows, there is a way to find what the default browser is. You will have to use the commands

REG QUERY HKCR\.html /ve

which would return

! REG.EXE VERSION 3.0

HKEY_CLASSES_ROOT\.html
    <NO NAME>   REG_SZ  FirefoxHTML

Then you would query FirefoxHTML (or whatever value was returned) using the same command, and append the following to the key

REG QUERY HKCR\FirefoxHTML\shell\open\command /ve

and this would return

! REG.EXE VERSION 3.0

HKEY_CLASSES_ROOT\FirefoxHTML\shell\open\command
    <NO NAME>   REG_SZ  "C:\Program Files\Mozilla Firefox\firefox.exe" -requestPending -osint -url "%1"

From here, you can parse the returned lines to grab the location of the browser executable.

You can do all this using the Java Runtime class:

Runtime.getRuntime.exec(cmdString);

This requires a lot of customized coding, but you could essentially create your own API to access the default browser under Windows.

Here is an example of accessing the Windows registry in Java.

You could also search Google for more information on finding default browser in the registry.

As for other platforms (Mac, Linux, etc), I am sure there is a specific way to get the path to the default browser.

Hope this helps!

Jose Chavez
I don't think that answers the question. The question was not "How do I open a URL in the default browser?", it was "How do I open a URL in a new tab?"
Michael Myers
They can use this method to bring up a new browser window.
Jose Chavez
A: 

You will likely need to configure this in the browser, rather than in your launching code. You might be able to find some launch flags to cause the behavior you want, but you will be tying your code to a particular browser in the process.

JadeMason
+1  A: 

Have a look at BrowserLaunch2. In particular this method for forcing a new window to be opened.

Brian Agnew
A: 

It sounds like the java.awt.Desktop API doesn't give this level of functionality.

Here is a link we found to the java.net forums where this was asked in 2006.

A response there referred to JDIC's WebBrowser.

1moreriley
+1  A: 

You can use Yose method to identify which browser is default and then use the following commands:

firefox.exe -new-tab ie7 http://blogs.msdn.com/tonyschr/archive/2007/01/19/ie-automation-amp-tabs.aspx opera -newpage

Note that some browsers do not support tabs, or have no command line params to open an url in a tab. In that case you can use AHK scripting engine.

MateuszF