# [java] executing external programs from java

This topic is 4700 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I would like to make my java app run (in response to a user action) the predefined browser with a given url. What is the easiest way to do that?

##### Share on other sites
I don't know if I have an answer for that, but what would the URL look like? Say, a .jar on the end? Is this supposed to be from within a Java application? Or just in response to some arbitrary URL?

L-

##### Share on other sites
I think he means open a browser like IE and make it go to a specific page, such as google.com

##### Share on other sites
You may want to look into java web start application.
I havent used it, but from what I understand, it starts a java app when you go to a web page. Of course there is much more to this...but I think that is waht you want

##### Share on other sites
Okay then you'll want to have a look at this link:

When Runtime.exec() Won't

This will show you how to do a runtime call to an exe. Which in the case of IE should also accept a command line URL, and it does. On another OS, I'm sure it will work similarly (haven't tried it on a Unix box)... but generally something like this is about 100 times easier on a Unix box.

L-

##### Share on other sites
Quote:
 Original post by LucidquietOkay then you'll want to have a look at this link:When Runtime.exec() Won'tThis will show you how to do a runtime call to an exe. Which in the case of IE should also accept a command line URL, and it does. On another OS, I'm sure it will work similarly (haven't tried it on a Unix box)... but generally something like this is about 100 times easier on a Unix box.L-

This should be what I need. Tomorrow I will try it. By the way, is there a way to detect which is the default browser? This is not so important, but would be nice...
Thank you again!

EDIT: I tried to rate you up but it doesn't work. Am I restricted to only one time? That's the first time that this happens to me, so I don't know...

##### Share on other sites
Quote:
 Original post by cignox1EDIT: I tried to rate you up but it doesn't work. Am I restricted to only one time? That's the first time that this happens to me, so I don't know...

I don't know about that either -- but thanks nonetheless. :)

L-

##### Share on other sites
Well as said above you could use something like
Runtime.getRuntime().exec("iexplore www.website.com") but it has a lot of problems :
-you must detect the operating system and use a particular command depending on each os.
-What will happen with an exotic os (solaris/freebsd/...)?
-What if the command is not supported anymore later?
-In the windows case you can launch internet explorer but you can't launch (easaliy) the default browser.
-...

Fortunately there is a really nice java library which solve the problem: JDIC (Desktop Integration for Java Applications).
It's aim is to allow a better integration of java application with the operating system.
Here is the link : https://jdic.dev.java.net/

with jdic you can :
-Launch the system default browser
-embed a web browser INSIDE your application
-Launch the application associated with a given File
-Launch the default system mailing system such as outlook (think mailto:me@blah.com)
-Put your application in the system tray
And a lot of other nifty things.

##### Share on other sites
Some of these features are coming along with Mustang. Although it's not stable enough to produce software over it, you can plan a release on it depending on the time you have to deliver such code. But there's nothing wrong, and it might still be the safest approach, to use JDK 1.5 + JDIC.

Son of Cain

##### Share on other sites
i haven't used JDIC yet, but it sounds like it would be the way to go.

my (quite hackish) solution to the "launching the default app for something" problem (which only works under windows, but that didn't matter in the project i was using it) was the following (i used it for launching acroread for viewing a PDF, but it should be usable for things like the default browser also):

/** * Starts the default viewer, thats linked to the filetype * of the file <code>f</code>. * @param f The file to be opened. */public static void spawnFileProg(File f) {	if (f.exists() && f.isFile() && f.canRead()) {		// comspec is the name of the command line interpreter, you should		// really get it with something like:		//String comspec = System.getenv("COMSPEC");		// ...				String comspec = "cmd";		try {			Runtime.getRuntime().exec(				comspec + " &#47;c start \"Please wait...\" " + f.getName(),				null,				f.getParentFile());		} catch (IOException e) {			throw new RuntimeException(				"Couldn't start the viewer for: " + f,				e);		}	} else {		// File doesn't exist or isn't readable, do something about it!		// ...	}}

This uses the nifty "start" command that's present in most modern windows-cli's.

-Raven

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633660
• Total Posts
3013224
×