# Default Link in Swing, --> öffne Browser -> Browse URL



## peppi (10. Nov 2003)

Hi Leute,

ich möchte einen ganz normalen Internetüblichen Link auf meine Swing-Fenster setzen, die den Defaul-Browser aufrufen und entsprechende URL aufrufen.

Default Link in Swing, --> öffne Browser -> Browse URL

Kann einfach nix dazu in den Dokus finden. Kann mir da jemand helfen?

 :?:


----------



## bygones (10. Nov 2003)

```
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler http://www.google.de")
```

Sollte für Windows funktionieren, wie es unter Linux u.ä. geht weiß ich nicht


----------



## jptc.org (12. Nov 2003)

Versuchs mal so, das funktioniert nicht nur unter Windows:


```
package org.jptc.logic;

/* Copyright (C) 2002 */
/* This class was found at: [url]http://www.javaworld.com[/url] */

import java.io.IOException;

/**
* A simple, static class to display a URL in the system browser. 

*
* Under Unix, the system browser is hard-coded to be 'netscape'.
* Netscape must be in your PATH for this to work. This has been
* tested with the following platforms: AIX, HP-UX and Solaris. 

*
* Under Windows, this will bring up the default browser under windows,
* usually either Netscape or Microsoft IE. The default browser is
* determined by the OS. This has been tested under Windows 95/98/NT. 

*
* Examples: 

* 
BrowserControl.displayURL("http://www.javaworld.com")
* 
BrowserControl.displayURL("file://c:\\docs\\index.html")
* 
BrowserContorl.displayURL("file:///user/joe/index.html");
* 

* Note - you must include the url type -- either "http://" or
* "file://".
*/
public class BrowserControl {
    // Used to identify the windows platform.
    private static final String WIN_ID = "Windows";
    // The default system browser under windows.
    private static final String WIN_PATH = "rundll32";
    // The flag to display a url.
    private static final String WIN_FLAG = "url.dll,FileProtocolHandler";
    // The default browser under unix.
    private static final String UNIX_PATH = "netscape";
    // The flag to display a url.
    private static final String UNIX_FLAG = "-remote openURL";
    /**
    * Display a file in the system browser. If you want to display a
    * file, you must include the absolute path name.
    *
    * @param url the file's url (the url must start with either "http://"
    or
    * "file://").
    */
    public static void displayURL(String url) {
        boolean windows = isWindowsPlatform();
        String cmd = null;
        try {
            if (windows) {
                // cmd = 'rundll32 url.dll,FileProtocolHandler http://...'
                cmd = WIN_PATH + " " + WIN_FLAG + " " + url;
                Runtime.getRuntime().exec(cmd);
            } else {
                // Under Unix, Netscape has to be running for the "-remote"
                // command to work. So, we try sending the command and
                // check for an exit value. If the exit command is 0,
                // it worked, otherwise we need to start the browser.
                // cmd = 'netscape -remote openURL([url]http://www.javaworld.com[/url])'
                cmd = UNIX_PATH + ' ' + UNIX_FLAG + '(' + url + ')';
                Process p = Runtime.getRuntime().exec(cmd);
                try {
                    // wait for exit code -- if it's 0, command worked,
                    // otherwise we need to start the browser up.
                    int exitCode = p.waitFor();
                    if (exitCode != 0) {
                        // Command failed, start up the browser
                        // cmd = 'netscape http://www.javaworld.com'
                        cmd = UNIX_PATH + " " + url;
                        p = Runtime.getRuntime().exec(cmd);
                    }
                } catch (InterruptedException x) {
                    System.err.println("Error bringing up browser, cmd='" + cmd + "'");
                    System.err.println("Caught: " + x);
                }
            }
        } catch (IOException x) {
            // couldn't exec browser
            System.err.println("Could not invoke browser, command=" + cmd);
            System.err.println("Caught: " + x);
        }
    }
    /**
    * Try to determine whether this application is running under Windows
    * or some other platform by examing the "os.name" property.
    *
    * @return true if this application is running under a Windows OS
    */
    public static boolean isWindowsPlatform() {
        boolean retVal = false;
        String os = System.getProperty("os.name");
        if (os != null && os.startsWith(WIN_ID)) {
            retVal = true;
        } else {
            retVal = false;
        }
        return retVal;

    }
}
```

Karsten Voigt
http://www.java-performance-portal.org


----------



## peppi (12. Nov 2003)

hey danke,

das probiere ich gleich mal aus.


----------



## peppi (12. Nov 2003)

:wink:  Hey danke,

funtioniert einwandfrei, damit kann ich echt was anfangen.

Gruß Pascal


----------



## Tec (21. Dez 2005)

@jptc.org
Funzt der Code nun nur unter Windows und Unix oder auch unter Linux ?


----------



## AlArenal (21. Dez 2005)

Tec hat gesagt.:
			
		

> @jptc.org
> Funzt der Code nun nur unter Windows und Unix oder auch unter Linux ?





> *
> * Under Unix, the system browser is hard-coded to be 'netscape'.
> * Netscape must be in your PATH for this to work. This has been
> * tested with the following platforms: AIX, HP-UX and Solaris.
> ...


----------



## Tukuan (28. Feb 2006)

jptc.org hat gesagt.:
			
		

> Versuchs mal so, das funktioniert nicht nur unter Windows:



Funktioniert das denn auch unter Linux?
Oder wie geht das denn?

Danke


----------



## maxf (20. Jul 2006)

Ich habe mir folgende Klasse gemacht:

```
import java.io.IOException;

public class BrowserControl {
	private String command = null;
	private String command2 = "";
	public final String[] browsers = {"Internet Explorer", "Internet Explorer(neues Fenster)", "Andere(z.B. Mozilla Firefox/Linux-Derivate)"};
	
	public BrowserControl(int browser, String path) throws UnknownBrowserException{
		switch(browser) {
			case 0: {
				command = "rundll32 url.dll,FileProtocolHandler ";
				break;
			}
			
			case 1: {
				command = "rundll32 url.dll,FileProtocolHandler javascript:location.href='";
				command2 = "'";
				break;
			}
			
			case 2: {
				command = path;
				break;
			}
			
			default: throw new UnknownBrowserException("Unkown Browser"); 
		}
	}
	
	public void showPage(String url) throws IOException{
		Runtime.getRuntime().exec(command+ " "+ url+ command2);
	}
}
```
Läuft unter Windows ohne weiteres, unter Linux muss man halt den Pfad angeben.


----------

