Visit url in webbrowser from a link from my application

Hello,

I have a java application. I would like to send my users to a specific URL for example "www.google.com". How do I do that?

I've got this great answere:

import java.lang.reflect.Method;

import javax.swing.JOptionPane;

publicclass test{

public test(){

}

publicstaticvoid main(String[] args){

String osName = System.getProperty("os.name");

String url ="http://www.google.com/";

try{

if (osName.startsWith("Mac OS")){

Class fileMgr = Class.forName("com.apple.eio.FileManager");

Method openURL = fileMgr.getDeclaredMethod("openURL",

new Class[]{String.class});

openURL.invoke(null,new Object[]{url});

}

elseif (osName.startsWith("Windows"))

Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);

else{//assume Unix or Linux

String[] browsers ={

"gnome-open","firefox","opera","konqueror","epiphany","mozilla","netscape"};

String browser =null;

for (int count = 0; count < browsers.length && browser ==null; count++)

if (Runtime.getRuntime().exec(

new String[]{"which", browsers[count]}).waitFor() == 0)

browser = browsers[count];

if (browser ==null)

thrownew Exception("Could not find web browser");

else

Runtime.getRuntime().exec(new String[]{browser, url});

}

}

catch (Exception e){

JOptionPane.showMessageDialog(null, e.getLocalizedMessage());

}

}

}

The problem

But do you know how I can make it open in the current open webpage after the first time? So its like refreshing the website.

Because now it opens a new tab for every time and I will end up with hundreds of thoose.

[3788 byte] By [tolvana] at [2007-11-26 22:42:21]
# 1

Continuation of this post:

http://forum.java.sun.com/thread.jspa?threadID=5150750&messageID=9566398#9566398

The code you have in front of you is browser independent. The solution you're asking for is browser-dependent. If you're using firefox, you're going to have to tweak the solution you've got in front of you to use that specific browser.

For firefox (and most other browsers), there are switches (-t?), depending on what version you use, that will allow you to open the link in a new tab. Presumably, there are switches that will tell it to also use the current window. However, I don't know what OS or browser you're talking about.

kevjavaa at 2007-7-10 11:57:49 > top of Java-index,Java Essentials,Java Programming...
# 2
I understand. I will use if for Windows XP and Internet Explorer 7
tolvana at 2007-7-10 11:57:49 > top of Java-index,Java Essentials,Java Programming...
# 3

Quick googling for command line options for IE 7 didn't give me anything substantial; this was the best I could come up with:

http://samanathon.com/2007/02/28/internet-explorer-command-line-switches/

I know there's a way to do it with Firefox, but with IE, I'm afraid you might be stuck, unless there's some OLE/COM way to do it. Those ways would be ugly under Java.

kevjavaa at 2007-7-10 11:57:49 > top of Java-index,Java Essentials,Java Programming...
# 4

import java.awt.Desktop; //Java 1.6

import java.io.IOException;

import java.net.URI;

import java.net.URISyntaxException;

public class BrowseExample {

public static void main(String[] args) throws IOException, URISyntaxException {

URI uri = new URI("http://java.sun.com");

Desktop.getDesktop().browse(uri);

}

}

DrLaszloJamfa at 2007-7-10 11:57:49 > top of Java-index,Java Essentials,Java Programming...
# 5
This one also opens the new website in a tab. I want it to refresh the current open webpage.
tolvana at 2007-7-10 11:57:49 > top of Java-index,Java Essentials,Java Programming...
# 6
Then reconfigure your browser so it works that way. If you have your browser set so that new pages open in a tab, then that's what will happen.
DrClapa at 2007-7-10 11:57:49 > top of Java-index,Java Essentials,Java Programming...