简体   繁体   中英

How to run bat file from java with arguments (i.e file name with full path) having folder name with space

Am trying to execute the a bat file with some arguments through a JAVA programmes . the arguments are file name with full path, And this path had some folder name with space, which are creating issue and giving me the following error

Error: 'D:\\Documents' is not recognized as an internal or external command

the code is as below

String command = "D:\Documents and Settings\ A.bat" + " " D:\Documents and Settings\B.xml



 1. process = Runtime.getRuntime().exec(new String[] {"cmd.exe","/c",command});
 2. process.waitFor();
 3. exitValue = process.exitValue();

You need to escape the \\ in your string (ie doubling them: D:\\\\Documents ), but that is not the problem. You can try to escape the spaces Documents\\\\ and\\\\ Settings or you use the exec method that does this for you. Just dont build the command line by yourself. Better use ProcessBuilder for starting processes.

I was trying to do the same thing. I googled whole day but didn't make it work. At Last I handled it in this way, I am sharing it if it comes to any use of anybody :

        String command = "A.bat D:\\Documents and Settings\\B.xml";
        File commandDir = new File ( "D:\\Documents and Settings ");        
        String[] cmdArray = { "cmd.exe", "/c", command };


        1. Process process = Runtime.getRuntime().exec( cmdArray, null, cmdArray );
        2. process.waitFor();
        3. exitValue = process.exitValue();

I've spent a while searching on SO and the wider Internet and was about to post this as a new question when I came across this, which does seem identical to my issue...

I am trying to call a Windows batch file from Java. The batch file takes several arguments but just the first, which is a path to a data file, is of relevance to this problem. The cut-down command line that I have been experimenting with is essentially:

cmd /c c:\path\to\my\batchfile.bat c:\path\to\my\datafile.mdl

I'm using Apache Commons Exec which ultimately delegates to Runtime.getRuntime().exec(String[] cmdarray, String[] envp, File dir) , the 'correct' version as opposed to the overloaded versions taking a single String command. Quoting of the arguments when they contain spaces is therefore taken care of.

Now, both the path to the batch file and/or the path to the data file can have spaces in them. If either the path to the batch file or the path to the data file have spaces in, then the batch file is executed. But if both have spaces in them then the path to the batch file is truncated at the first space.

This has to be a (Java or Windows?) bug, right? I've debugged right down to the native call to create() in java.lang.ProcessImpl and all seems ok. I'm on JDK1.6.

String command = "\"D:\Documents and Settings\\" A.bat" + " \"D:\Documents and Settings\B.xml\""

转义双引号,因此您可以在文字中包括双引号,以得到:

cmd.exe /x "D:\Documents and Settings\" A.bat "D:\Documents and Settings\B.xml"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM