Note that there are some explanatory texts on larger screens.

plurals
  1. POExecuting an external executable in a working directory containing spaces in Java?
    primarykey
    data
    text
    <p>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 <code>runTime.getRuntime().exec(args,null,dir)</code> for example where dir is a File. This works excellent but not when the working directory contains a space. As a short example:</p> <pre><code>File dir=new File("/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(); </code></pre> <p>Will not work, while using: <code>Process pr = Runtime.getRuntime().exec(cmd);</code> instead will launch the command successfully but leave me with all of the files in the jars working directory. Using processbuilder instead and using p<code>rocessbuilder.directory(dir);</code> to set the directory exits with the message:</p> <pre><code>Exception: Cannot run program "/Users/chargedPeptide/rar/unrar" (in directory "/Users/chargedPeptide/rar 2"): error=2, No such file or directory </code></pre> <p>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.</p> <p>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.</p> <p>Any ideas most welcome.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload