Obscure Java Question

Discussion in 'Mac Programming' started by hsvmoon, Apr 24, 2007.

  1. hsvmoon macrumors newbie

    Joined:
    Jul 31, 2006
    Location:
    Huntsville Al
    #1
    I have a File object that points to an executable. I also have a File object that points to a text file. I need to execute the program with the text file as an argument. I am using the exec method of the Runtime class to do this, but when I try to escape any spaces in the paths I get two backslashes in the command and it fails. If I print the command out it looks fine. Any Ideas?
    Code:
    String exe = exeFile.getAbsolutePath().replace(" ","\\ ");
    String file = inFile.getAbsolutePath().replace(" ","\\ ");
    Process p = Runtime.getRuntime().exec(exe + " " + file);
    
    the regex version replaceAll yeilds the same results
     
  2. hsvmoon thread starter macrumors newbie

    Joined:
    Jul 31, 2006
    Location:
    Huntsville Al
    #2
    platform work around

    If I quote the exe path and the input path indiviually it works on win32. If place the command fragments into a string array and use the array based Runtime method it works on unix systems. I would love to hear any platform independant solutions. Looking on sun's site this is a known issue.
     
  3. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #3
    How about:-

    Code:
    exeFile.getAbsolutePath().replaceAll( "\\s","\\\\");
    That should replace all spaces with '\ '.

    b e n
     
  4. iSee macrumors 68040

    iSee

    Joined:
    Oct 25, 2004
    #4
    Put the command fragments in an array and enquote each one :D .
     
  5. hsvmoon thread starter macrumors newbie

    Joined:
    Jul 31, 2006
    Location:
    Huntsville Al
    #5
    Thats is what i did in the first iteration. It yeilds the exact same results as the above code. Thanks for the idea.
     
  6. hsvmoon thread starter macrumors newbie

    Joined:
    Jul 31, 2006
    Location:
    Huntsville Al
    #6
    I tried this and it breaks the code on unix systems, because the quotes are always included around each command segment.

    what I have right now is

    Code:
    if(windows)
    {
    //use the quotes in the command array
    }
    else
    {
    //use no quotes in the command array
    }
    
    I just hate to use platform code when I can avoid it given the nature of my work.
     
  7. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #7
    Can't you use System.getProperty("file.separator") to get the different file separators for the different platforms?

    Or am I totally barking up the wrong tree :confused:.
     

Share This Page