简体   繁体   English

如何使用带有空格的文件夹名称的参数(即具有完整路径的文件名)从Java运行bat文件

[英]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 . 我正在尝试通过JAVA程序执行带有一些参数的bat文件。 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 错误:“ D:\\ Documents”未被识别为内部或外部命令

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. 您需要转义字符串中的\\ (即,将它们加倍: D:\\\\Documents ),但这不是问题。 You can try to escape the spaces Documents\\\\ and\\\\ Settings or you use the exec method that does this for you. 您可以尝试转义Documents\\\\ and\\\\ Settings空格Documents\\\\ and\\\\ Settings或者使用为您执行此操作的exec方法 Just dont build the command line by yourself. 只是不要自己构建命令行。 Better use ProcessBuilder for starting processes. 更好地使用ProcessBuilder来启动进程。

I was trying to do the same thing. 我试图做同样的事情。 I googled whole day but didn't make it work. 我整天用Google搜索,但没有成功。 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... 我花了一段时间在SO和更广泛的Internet上进行搜索,并在遇到此问题时将其发布为新问题,这似乎与我的问题相同...

I am trying to call a Windows batch file from Java. 我正在尝试从Java调用Windows批处理文件。 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. 我正在使用Apache Commons Exec,后者最终将其委托给Runtime.getRuntime().exec(String[] cmdarray, String[] envp, File dir) ,这是“正确”的版本,而不是采用单个String命令的重载版本。 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? 这一定是(Java或Windows?)错误,对吗? I've debugged right down to the native call to create() in java.lang.ProcessImpl and all seems ok. 我已经调试到java.lang.ProcessImplcreate()的本地调用,一切似乎都还可以。 I'm on JDK1.6. 我正在使用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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从sourceforge下载文件[即没有特定的文件名] - Downloading a file from sourceforge [i.e No specific file name] Java Servlet和JSP,如何从文件获取完整路径名? - Java Servlet and JSP, How to get full path name from a file? 如何从Java中的文件夹命名文件? - How to name a file from a folder in java? 如何在应用程序中隐藏jsp文件夹路径和文件名 - How Can I Hide jsp folder path and file name in application 使用 Java 将文件复制到具有相同文件名的文件夹中 - Copy file into a folder having same file name using Java 如何使用来自java程序的参数运行bat文件? (尝试使用命令行工具自动化) - How to run bat file with arguments from a java program? (Trying to automate using a Command Line Tool ) 从Java中的文件名获取文件路径 - Getting the file path from file name in java 如何在java中获取输出流的名称(即stderr或stdout)? - How to get name of output stream (i.e, stderr, or stdout) in java? 是否可以仅使用文件名和参数来运行Java文件? - Is it possible to run a java file with just the file name and arguments? 在部署简单的Web应用程序时,java文件(即servlet)不在服务器上运行(Tomcat) - While deploying a simple web application, the java file (i.e servlet) doesn't run on server (Tomcat)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM