 |
java.awt.Desktop vs Runtime.exec()
Posted by ixmal on May 10, 2006 at 04:19 PM | Comments (5)
Desktop API vs Runtime.exec()
I'm often asked why is that new class, java.awt.Desktop, is introduced. They state all the required actions can be performed with the call to one of Runtime.exec() methods:
Desktop.getDesktop().open(new File(filename));
is close to
Runtime.getRuntime().exec("start " + filename);
So I decided to write a couple of words about this issue to answer the most of such questions at once:)
The difference is...
The problem with the "start" invocation is that it only works on the Windows platform. It will fail on the Mac, Linux, and Solaris platforms. With the Desktop API, you can be guaranteed that it will start an application on all platforms.
There are also lots of other handy features in the new API, such as open, edit, print, and two special actions - browse and mail - that launch separate processes.
See java.awt.Desktop JavaDoc for details.
Bookmark blog post: del.icio.us Digg DZone Furl Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment
-
AFAIK, "start" isn't even available on Windows 9x. It's an NT/XP only thing.
"start" doesn't even work on OS X--it's "open".
Posted by: afishionado on May 11, 2006 at 11:02 AM
-
I am looking at the java.awt.Desktop docs now.
Why do some of the methods take FileS instead of URIs?
Why is there no mechanism to communicate with the editor/browser/printer/mailer? Instead of returning void, a Process or some new abstraction could be returned.
Is the intent to add more desktop functionality in the future? Otherwise, a name like PlatformApplicationLauncher would be more appropriate. The term desktop brings to mind something more like this:
The Microsoft Windows user interface (UI) provides users with access to a wide variety of objects necessary for running applications and managing the operating system. The most numerous and familiar of these objects are the folders and files that reside on computer disk drives. There are also a number of virtual objects that allow the user to do tasks such as sending files to remote printers or accessing the Recycle Bin. The Shell organizes these objects into a hierarchical namespace, and provides users and applications with a consistent and efficient way to access and manage objects.
Shell Programmer's Guide
Posted by: coxcu on May 11, 2006 at 05:40 PM
-
Very cool!
Using 'start' on windows is version dependent. In Windows XP there is not even a start.exe per say, but a command inside of cmd.exe, cmd.exe /start or some such thing. When using it you have to watch out for quoting of paramaters and paramaters of paramaters. It's a pain.. I wrote a small C program that was basically a call to the Windows API function ShellExecute(). That worked better. Then I had to write a wrapper class that deligated to my runner.exe, or open on a Mac, and to some custom shell script on Linux. What do you guys do for Linux/Unix/Solaris? Is it desktop environment dependant?
Posted by: aberrant on May 11, 2006 at 08:09 PM
-
To coxcu: there is already an RFE about ability to get a java.lang.Process when using Desktop API, see 6345777 for details.
As for further java.awt.Desktop changes, as I know it won't be changed in Mustang but will probably provide more functionality in the next Java 2 SE releases.
Posted by: ixmal on May 12, 2006 at 10:06 AM
-
To alberrant:
You're right, java.awt.Desktop implementation does depend on current desktop environment. Currently it is supposed to work with Gnome, on Linux and Solaris 9+, other environment may or may not work. This is what Desktopn.isDesktopSupported() method is intended for
Posted by: ixmal on May 12, 2006 at 10:13 AM
|