PDA

View Full Version : Obscure Java Question




hsvmoon
Apr 24, 2007, 11:44 PM
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?
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
Apr 25, 2007, 12:40 AM
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.

lazydog
Apr 25, 2007, 07:54 AM
How about:-

exeFile.getAbsolutePath().replaceAll( "\\s","\\\\");

That should replace all spaces with '\ '.

b e n

iSee
Apr 25, 2007, 09:27 AM
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
Apr 25, 2007, 09:39 AM
How about:-

exeFile.getAbsolutePath().replaceAll( "\\s","\\\\");

That should replace all spaces with '\ '.

b e n

Thats is what i did in the first iteration. It yeilds the exact same results as the above code. Thanks for the idea.

hsvmoon
Apr 25, 2007, 09:44 AM
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


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
Apr 25, 2007, 11:20 AM
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:.