通过ProcessBuilder将字符串传递给批处理文件

时间:2013-09-26 03:13:41

标签: java batch-file format

我正在使用Java ProcessBuilder将String作为参数传递给批处理文件。

  

ProcessBuilder pb = new ProcessBuilder(                   “batch.bat”                   “jason mary molly”);

批处理文件......

 @ECHO OFF
 SET V1=%1
 ECHO %V1%
 pause

批处理文件输出为此(注意双引号):

“jason mary molly”

如果我只输入1个字符串,则找不到引号!我的问题是,我有一个相当复杂的程序,我写的要求这3个参数没有引号,或者它们将被程序视为一个文件名而不是3个单独的参数。有没有办法删除这些双引号?

2 个答案:

答案 0 :(得分:2)

尝试:%~1它应该删除引号

有关详细信息,请在cmd中键入:for /? | more +121。这是引用参考:

%~I         - expands %I removing any surrounding quotes (")
%~fI        - expands %I to a fully qualified path name
%~dI        - expands %I to a drive letter only
%~pI        - expands %I to a path only
%~nI        - expands %I to a file name only
%~xI        - expands %I to a file extension only
%~sI        - expanded path contains short names only
%~aI        - expands %I to file attributes of file
%~tI        - expands %I to date/time of file
%~zI        - expands %I to size of file
%~$PATH:I   - searches the directories listed in the PATH
               environment variable and expands %I to the
               fully qualified name of the first one found.
               If the environment variable name is not
               defined or the file is not found by the
               search, then this modifier expands to the
               empty string

可以组合修饰符以获得复合结果:

%~dpI       - expands %I to a drive letter and path only
%~nxI       - expands %I to a file name and extension only
%~fsI       - expands %I to a full path name with short names only
%~dp$PATH:I - searches the directories listed in the PATH
               environment variable for %I and expands to the
               drive letter and path of the first one found.
%~ftzaI     - expands %I to a DIR like output line

答案 1 :(得分:1)

传递给String的每个ProcessBuilder都会成为您正在执行的命令的参数,因此使用

ProcessBuilder pb = new ProcessBuilder( "batch.bat", "jason mary molly");

表示您将 1 参数传递给您的命令,而不是三个。

尝试使用...

ProcessBuilder pb = new ProcessBuilder( "batch.bat", "jason", "mary", "molly");

相反