Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

hsvmoon

macrumors newbie
Original poster
Jul 31, 2006
24
0
Huntsville Al
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
 

hsvmoon

macrumors newbie
Original poster
Jul 31, 2006
24
0
Huntsville Al
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.
 

iSee

macrumors 68040
Oct 25, 2004
3,540
272
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.

Put the command fragments in an array and enquote each one :D .
 

hsvmoon

macrumors newbie
Original poster
Jul 31, 2006
24
0
Huntsville Al
Put the command fragments in an array and enquote each one :D .
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.
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
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:.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.