We help IT Professionals succeed at work.

Launch Web Browser in an application?

GloriousRain
GloriousRain asked
on
Dear all,
How can i launch Web Browser in an Java application(not applet)?
Comment
Watch Question

Commented:
import java.util.*;
import java.io.*;
import java.text.*;

public class Test{

  public void runExplorer()throws Exception{
    Runtime temp=Runtime.getRuntime();

    temp.exec("c:\\progra~1\\Intern~1\\IExplore.exe");
  }
  public static void main(String[] args)throws Exception{
     Test a=new Test();
     a.runExplorer();
   
  }
}


Try this... hope this will help you.

Commented:
May be you no need to import those packages..
You try to test the program by remove them one by one.
Ok. Good Luck

Author

Commented:
yongsing,
i couldn't open your link.
yongyih,
i don't want hardcode in my app. Is the address of IE always in the path you address? If not, how can i get the path in runtime?

Author

Commented:
yongyih,
one more thing: how about if i use Netscape?

Commented:
This link works fine for me here. Why don't you try again?

To run Netscape:

Runtime.getRuntime().exec(
"C:\Program Files\Netscape\Communicator\Program\netscape.exe http://www.yahoo.com");

This will open Netscape with the URL to Yahoo!

Commented:
The link will teach you how to launch the default browser on Windows platform.

http://www.javaworld.com/javaworld/javatips/jw-javatip66.html

Author

Commented:
Dear all,
it's better if all you guys can give me the way to get the path for IE and Netscape because i don't want hardcode in my app.

Commented:
GloriousRain, please refer to the URL I've given. I believe it teaches you how to launch the default browser in your system.

Author

Commented:
:) yongsing,
i still can't open. but i tell u the true, that i've found code in own our project shows how to launch Web in Java App, the code indicates it has origin from your link ( that reason y i give u points ). Now it's still not works fine because some difficult about multi OS problem. Thanks anyway!

Commented:
Great to hear that you already found your answer ^^
Anyway, if don't want to hardcode it, just pass in the path as parameter..

Like this, you can run any exe file you want. ^^

public class Test{
 public void runProgram(String strPath)throws Exception{
   Runtime temp=Runtime.getRuntime();
   temp.exec(strPath);
 }
 public static void main(String[] args)throws Exception{
  Test a=new Test();
  a.runProgram("c:\\progra~1\\Intern~\\IExplore.exe");  
 }
}


Author

Commented:
yongyih,
"a.runProgram("c:\\progra~1\\Intern~\\IExplore.exe");"
->This is also hardcode man. I mean you can get the path from Registry or somewhere at runtime not code strings directly in your program at design time.  

Explore More ContentExplore courses, solutions, and other research materials related to this topic.