开发者

Executing an external executable in a working directory containing spaces in Java?

I have a specific need to unrar files found in different subdirectories during execution of my program on Os x. I do this by calling the freeware command line tool unrar, which works very well. However, unrar will always unrar files in the current working directory, which means that I have to specify a working directory for the executed process or get every unpacked file in my .jar home folder. This is easy enough to do using either the processBuilder.directory(dir) or runTime.getRuntime().exec(args,null,dir) for example where dir is a File. This works excellent but not when the working directory contains a space. As a short example:

File dir=new File(开发者_如何学Python"/Users/chargedPeptide/rar 2");
String[] cmd = { "/Users/chargedPeptide/rar/unrar", "e", "-o+","/Users/chargedPeptide/rar", "2/weather.rar"};
Process pr = Runtime.getRuntime().exec(cmd,null,dir);
int exitValue=pr.waitFor();

Will not work, while using: Process pr = Runtime.getRuntime().exec(cmd); instead will launch the command successfully but leave me with all of the files in the jars working directory. Using processbuilder instead and using processbuilder.directory(dir); to set the directory exits with the message:

Exception: Cannot run program "/Users/chargedPeptide/rar/unrar" (in directory "/Users/chargedPeptide/rar 2"): error=2, No such file or directory

Help? How do I handle the spaces? I've tried adding backslashes before the spaces to make them literal no help since the File object treats them like actual part of the path.

Edit: To make the whole thing a bit more clear: 1. I have a separate method that feeds the execute method a command and a directory to processbuilder, all directories are found by the previous method and exist. This works except when the dir contains a space. 2.I need to set the working dir or the command will execute in the wrong place. 3.Parsing the file object by: dir=new File(dir.tostring.replace(" ","\ "); to put a backslash in front of all spaces does not work since the java File object then looks for a path containing actual backslashes. No luck. 4.Using rt.exec instead of processbuilder dosen't help either.

Any ideas most welcome.


How about:

dir.mkdirs();

before launching the process.

This creates the missing directory.

Edit:

This looks strange.

String[] cmd = { "/Users/chargedPeptide/rar/unrar", "e", "-o+","/Users/chargedPeptide/rar", "2/weather.rar"};

If this was a shell you'd write:

unrar e -o+ "/Users/chargedPeptide/rar 2/weather.rar"

You actually have to put the .rar file in quotes, since otherwise it will be interpreted as 2 arguments to the process.

The way you've split 'cmd' will do exactly that, break the rar argument in two. Try:

String[] cmd = { "/Users/chargedPeptide/rar/unrar", "e", "-o+","/Users/chargedPeptide/rar 2/weather.rar"};


Not sure if it will work, but can you try putting a / at the end of the path. i.e.,

File dir=new File("/Users/chargedPeptide/rar 2/");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜