Open a file - Mac

J

JScoobyCed

Hi,

A few months ago, a post was asking how we can open a document on a Mac
OS X from a Java application.
Since wednesday I try to use all the methods proposed and found on
internet, without success.
I would like to (for example) open a PDF file in the PDF Viewer (or any
program registered to open PDF file).
I have triied:
Runtime.getRuntime().exec("open \"/Volumes/Tiger/user1/Doc1.pdf\"");

or
String[] c = new String[]{"open", "\"/Volumes/Tiger/user1/Doc1.pdf\""};
Runtime.getRuntime().exec(c);

and even using the MRJUtils.jar package:
File file = MRJApplicationUtils.findApplicationHandler(new
MRJOSType("pdf"));
String cmd = file.getPath() + "\"/Volumes/Tiger/user1/Doc1.pdf\"";
Runtime.getRuntime().exec(cmd);

(also triied with array cmd = new String[] {file.getPath(), ...} )

Somebody please help :) I don't want to open only PDF, but DOC, PPT,
GIF, HTML, any file registered to the OS (eventually, with the
MRJUtils.jar, I can register new file to programs, though I didn't test
yet :) )

Thank you
 
J

John B. Matthews

JScoobyCed said:
I would like to (for example) open a PDF file in the PDF Viewer (or
any program registered to open PDF file). I have tried:
Runtime.getRuntime().exec("open \"/Volumes/Tiger/user1/Doc1.pdf\""); [...]
JSC

Works OK for me. You might examine the error stream to see where it's
getting lost. Also, you may need to use -a in your command line to
specify the application.

import java.io.*;
class ExecTest {

public static void main (String[] args) {
String s;
try {
Process p = Runtime.getRuntime().exec(
"open /Documents/StudioDisplay.pdf");
BufferedReader err = new BufferedReader (
new InputStreamReader (p.getErrorStream()));
while ((s = err.readLine()) != null) {
System.out.println(s);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
 
S

Steve W. Jackson

JScoobyCed said:
Hi,

A few months ago, a post was asking how we can open a document on a Mac
OS X from a Java application.
Since wednesday I try to use all the methods proposed and found on
internet, without success.
I would like to (for example) open a PDF file in the PDF Viewer (or any
program registered to open PDF file).
I have triied:
Runtime.getRuntime().exec("open \"/Volumes/Tiger/user1/Doc1.pdf\"");

or
String[] c = new String[]{"open", "\"/Volumes/Tiger/user1/Doc1.pdf\""};
Runtime.getRuntime().exec(c);

and even using the MRJUtils.jar package:
File file = MRJApplicationUtils.findApplicationHandler(new
MRJOSType("pdf"));
String cmd = file.getPath() + "\"/Volumes/Tiger/user1/Doc1.pdf\"";
Runtime.getRuntime().exec(cmd);

(also triied with array cmd = new String[] {file.getPath(), ...} )

Somebody please help :) I don't want to open only PDF, but DOC, PPT,
GIF, HTML, any file registered to the OS (eventually, with the
MRJUtils.jar, I can register new file to programs, though I didn't test
yet :) )

Thank you

In general, I don't think you want to use the first effort (a single
long string), since Mac OS X supports directory and file names with
spaces in them. I also don't think you need to use the MRJUtils package
-- and even if you do, I suspect that the MRJOSType would really need to
be a 4-character Mac OS "type code", which would probably mean it needs
the space at the end.

But here's something I've done recently that I happen to know for sure
works. If the string you have to open is a regular URL (be it http,
ftp, etc.), it can remain intact without trouble. If it's a file, then
use the Java File class call toURI() to create a "file:" URI from it.
Your string array will then need to contain two entries. The first will
contain "open", and the second will contain the string (URL or URI, with
no need for any quotes provided by you). Passing that to the exec()
method will get the desired reaction. The URL will open with your
system's default app for its type. The file will open with whichever
application is designated. Keeping in mind that not all files with the
same extension necessarily open with the same app, that could be Preview
or Adobe Reader, depending on your system and the selected file.

HTH.

= Steve =
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top