Xcode+Java - How to call an application's exe ?

Discussion in 'Mac Programming' started by Kathe, Oct 15, 2008.

  1. Kathe macrumors newbie

    Sep 19, 2008
    Hi All,

    Im writing Java in XCode.
    Can somebody provide me with the code snippets for calling an applications exe (say Microsoft word or Adobe InDesign etc)from Java swings.

  2. garethlewis2 macrumors 6502

    Dec 6, 2006
    ProcessBuilder and Runtime.exec.

    Look at those two in the Java documentation. That is all you will need to know to launch an application from inside Java.
  3. Kathe thread starter macrumors newbie

    Sep 19, 2008
    Thanks for ur reply.

    I triied the following code to open word 2004 application

    Runtime.getRuntime().exec("open /Applications/Microsoft Ofiice 2004/Microsoft Word")

    But this is not opening the Word exe since the folder name "Microsoft Ofiice 2004"contains space in between.

    How to go about this
  4. Guiyon macrumors 6502a

    Mar 19, 2008
    North Shore, MA
    First of all, I'd double check your spelling of 'Office'. As for the spaces, since they are being passed to the open command you will need to escape them for it to work properly; adding a '\\' (reduces down to a single '\' after compilation) before each space OR adding a '\"' to either side of the path should work.
  5. Kathe thread starter macrumors newbie

    Sep 19, 2008

    I tried the following,but it did not work

    Runtime.getRunTime().exec("open /Applications/Microsoft\\Ofiice\\2004/Microsoft\\Word")

    I tried this too ,but did not work

    Runtime.getRunTime().exec("open /Applications/Microsoft Ofiice 2004/Microsoft Word/")

    Can u pls code the correct way of using this
  6. charronegro50 macrumors newbie

    Feb 11, 2009
    Try replacing the spaces with %20 as in URLEncode, I think its the answer
  7. Sijmen macrumors 6502a

    Sep 7, 2005
    Any of these two:
    Runtime.getRunTime().exec("open /Applications/Microsoft\\ Office\\ 2004/Microsoft\\ Word.app");
    Runtime.getRunTime().exec("open \"/Applications/Microsoft Office 2004/Microsoft Word.app\"");
    In the first version, every space is escaped for the shell. This is done with the \ character. Because that character is reserved in Java strings, you'll have to espace it so it becomes \\. The mistake you made with your version was leaving the spaces out.

    In the second version, the path is quoted so the open command gets it as one parameter. The " is escaped using \ because it would otherwise end the string.

    By the way, as noted earlier you misspelled Office in that path, and .app is parth of the name of that directory. Also I think it isn't a very good idea to hardcode paths to apps.

    And you should have been able to find this out by yourself, really.
  8. mpcoder macrumors newbie

    Mar 21, 2009
    I've been trying to have a java application open a quicktime video, but its not working.

    I've tried:
    Runtime.getRuntime().exec("open \"/Applications/QuickTime Player.app\"");

    but no dice...

    any ideas why?
  9. venski macrumors newbie

    Apr 30, 2009
    You can try the following:
    String params[] = {
    	"/Applications/Microsoft Office 2004/Microsoft Word.app"
    This way you tell the JVM to pass the second string as a parameter to the open executable, and it takes care of escaping it in a proper way.

    It worked alright for me.

Share This Page