I am working on an issue where spaces in a directory cause a program that runs exp.exe to crash. The user first selects a .dmp file from any directory they want, which is then used to build an argument list. This argument list contains the command to be run, in this case, exp.exe, the .dmp file name, a log file created in the directory the file name comes from, along with some other parameters. ProcessBuilder is then used to do the following:
public static ProcessBuilder build;
build = new ProcessBuilder();
build.command(args);
build.redirectErrorStream(true);
process = build.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
When the directory the file comes from does not have any spaces, exp.exe runs as intended and line prints as expected. However, when the directory the file comes from does contain spaces, exp.exe does not run and terminates after outputting error code LRM-00101: unknown parameter name.
This is an example of a directory that does not work
C:\Users\Me\OneDrive - Folder\Desktop\test\log.dmp
A directory such as that one would be processed incorrectly and output
LRM-00101: unknown parameter name 'Folder\Desktop\test\log.dmp'
And this is an example of a directory that does run
C:\Users\Me\Downloads\test\log.dmp
In the example directory that does not work, ProcessBuilder is incorrectly getting the directory by treating everything before the "- " as not part of the directory, which is causing it to fail. I have already tried to escape the directory with double quotes when it is being read in initially by doing
"\"" dmp_filename "\""
but that does not work and other suggestions in similar threads do not seem to apply.
I would appreciate any help.
CodePudding user response:
Spaces in file or directory names have to escape by \!
String file = "C:\Users\Me\OneDrive - Folder\Desktop\test\log.dmp";
file = file.replaceAll(" ", "\\ ");
The double escaping isn't really nice!
CodePudding user response:
It looks like you are passing all arguments as a single String separated by spaces and with quotes for pathnames containing spaces. If so just switch to String[] and omit the escaping. For example switch:
String args = "app.exe \"path with space\" otherarg";
to:
String[] args = new String[] {
"app.exe", "path with space", "otherarg"
};
