Skip to main content

How to open a document through Java, on Mac

1 reply [Last post]
Joined: 2008-08-31


I know that there do exists a few similar threads about this subject already, but not for Mac.

The methods that I have tried so far are:
Runtime.getRuntime().exec("cmd /c \""+filepath+"\"");
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + filepath);

(Atleast the last two should work fine in Windows)

Since im running Mac osx 10.5.11 and only got Java 1.5 installed, I dont have access to:

I tried adding JDIC.jar to test if this would do the job. Im not sure there are some further initiation needed etc.. is there?

Anyways, using LaunchService() did not do the work either.

LaunchService ls = new LaunchService() {
}; File(filepath));

Returns the following error:
Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet.

So my question is: How do I open a document (in the OS) through Java on Mac?
("document" in my case is a .txt file)

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2003-06-10

#1 Avoid that form of exec() - always use the form that takes an array - that way you don't have to deal with quoting your filepath which you can easily get wrong since it is shell-specific behavior and runtime.exec() is not a shell.

#2 The command on OS X to open a file is (get ready) "open"
open blah.html
open blah.pdf