O'Reilly logo

Swing Hacks by Chris Adamson, Joshua Marinacci

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Open Files, Directories, and URLs on Mac OS X #80
Chapter 11, Native Integration and Packaging
|
411
HACK
As another example, cmd.exe /c start version.txt opens a text file in Win-
dows Notepad (assuming that’s the default text file viewer).
cmd.exe /c start
winword.exe version.txt
would open the file in MS Word. This requires
having Word installed, of course, so I would only recommend hardcoding
the program name if you are sure it’s really there.
Open a Directory
The start command can also open a directory as well as a file, meaning it
will open a file manager window showing the contents of that directory.
Program installers often do this after the install is complete to show where
the program was placed. The code in Example 11-4 will open a new
Explorer window showing the contents of the C: drive.
H A C K
#80
Open Files, Directories, and URLs
on Mac OS X Hack #80
Open files, directories, and URLs in external programs right from your Swing
app.
We can’t let Windows have all of the fun. Mac OS X has a similar and even
easier to use program called
open, which lets you open any file, directory, or
web page directly from the command line. This hack shows you how to
embed
open in your own program.
Using Open
Back in the late 80s, NeXT, Inc. created the first true integration between a
graphical user interface and a Unix-like operating system when they released
NeXTSTEP. Part of this OS was a command-line program called
open, which
could open a file, directory, and (once the Web was invented) a URL. Apple
purchased NeXT in the late 90s and NeXTSTEP became the core of Mac OS
X. Along with this purchase came the
open command, still as useful as ever.
Example 11-4. Opening a directory on Windows
import java.io.IOException;
public class ExecTest {
public static void main(String[] args) throws IOException {
String cmd = "cmd.exe /c start ";
String file = "c:\\";
Runtime.getRuntime( ).exec(cmd + file);
}
}

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required