[java] executing external programs from java

Started by
10 comments, last by cignox1 18 years, 2 months ago
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?
Advertisement
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-
"Education is when you read the fine print; experience is what you get when you don't." -Pete Seegerwww.lucid-edge.net
I think he means open a browser like IE and make it go to a specific page, such as google.com
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
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-
"Education is when you read the fine print; experience is what you get when you don't." -Pete Seegerwww.lucid-edge.net
Quote:Original post by Lucidquiet
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-


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...
Quote:Original post by cignox1
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...


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

L-

"Education is when you read the fine print; experience is what you get when you don't." -Pete Seegerwww.lucid-edge.net
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.
-Will your aplication still work with a newer version of windows/linux?
-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/
Another link since the above link seems to have problems at the moment http://javadesktop.org/articles/jdic/index.html

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.
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
a.k.a javabeats at yahoo.ca
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

This topic is closed to new replies.

Advertisement