Launching default webbrowser from java

Well. The title says it all. How do I do it? :slight_smile:

I can’t get Process.exec to work. In windows c++ you just spawn the entire URL and the system figures out the rest.

I.e. WinExec(“http://foobar.com”);

Thanks in advance :slight_smile:

Maybe you should try exec with the following String-array:

{ "cmd", "/c", "start", "http://www.foo.com" }

WiESi

[quote]Maybe you should try exec with the following String-array:

{ "cmd", "/c", "start", "http://www.foo.com" }

WiESi
[/quote]
It doesn’t look to be portable :frowning:

If the page you want to open is on your own server and can be restricted to vanilla html, you could always render it yourself. Here is some code for displaying an HTML based help system.

    /** Help Window */
    private class HelpWindow extends JFrame implements HyperlinkListener {
        private JEditorPane editorPane;

        /** Create a new Help Window */
        public HelpWindow(String title, java.net.URL helpURL) {
            super(title);
            editorPane = new JEditorPane();
            editorPane.setEditable(false);
            try {
                editorPane.setPage(helpURL);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            JScrollPane pane = new JScrollPane(editorPane);
            getContentPane().add(pane, java.awt.BorderLayout.CENTER);
            setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            addWindowListener(new java.awt.event.WindowAdapter() {
                public void windowClosing(java.awt.event.WindowEvent evt) {
                    formWindowClosing(evt);
                }
            });
            editorPane.addHyperlinkListener(this);
            setSize(600, 400);
            setVisible(true);
        }

        /** Process click on HTML hyperlink */
        public void hyperlinkUpdate(HyperlinkEvent event) {
            if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                try {
                    editorPane.setPage(event.getURL());
                } catch(IOException ioe) { }
            }
        }
    
        /** Inform parent that we have closed */
        private void formWindowClosing(java.awt.event.WindowEvent evt) {
            help = null;
        }
    }

Edit: Oh btw, this was an inner class. The last method nulled out the pointer to this window in the outer class when the window closed.

https://jdic.dev.java.net/
https://jdic.dev.java.net/nonav/documentation/javadoc/0.9/org/jdesktop/jdic/desktop/Desktop.html#browse(java.net.URL)

From an applet, you can use the applet context to open a URL

From java web start, you can use the BasicService (JNLP API) which provides the same function.

Lilian

Windows box can always use rundll32.exe utility to open a html page in a default browser. Should work in Win9X/Win2k/XP machines.

However, this might open the page in a currently running browser instance if one is found. I don’t know how to force always use a new instance window.

Run the following code with Runtime.exec methods.


rundll32.exe url.dll,FileProtocolHandler http://www.javasoft.com

Of course, you could always use the SEARCH button at the top of the page to find out about the previous lengthy discussion of this subject…

The search button? That’s crazy talk ;D

The JDIC project one to look into.

I strongly advise against just dropping a command to your shell – great way to make your program only work on one platform (and sometimes break even on different versions of that platform).

I use the good 'ol BrowserLauncher code

http://jtank.net/src/showsrc.php?src=src/net/jtank/util/BrowserLauncher.java

Works great for me, although I replaced “netscape” with “mozilla” for linux. Probably should update that with “firefox” now…

Will.

We use Sys.openURL() in LWJGL. Code’s mostly Java.

Cas :slight_smile:

The tank code worked perfectly. Actually it opens firefox on windows if it’s the default browser :slight_smile: