Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2005

Tip: Looking for answers? Try searching our database.

Open a file - Mac

Thread view: 
JScoobyCed - 24 Jun 2005 02:58 GMT
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

--
JSC
John B. Matthews - 24 Jun 2005 04:20 GMT
[...]
> 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();
       }
   }
}
Signature

John
jmatthews at wright dot edu
www dot wright dot edu/~john.matthews/

Steve W. Jackson - 24 Jun 2005 16:50 GMT
> Hi,
>
[quoted text clipped - 28 lines]
> --
> JSC

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 =
Signature

Steve W. Jackson
Montgomery, Alabama



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.