Java 在ProcessBuilder中添加双引号符号
我正在使用Java 在ProcessBuilder中添加双引号符号,java,bash,shell,Java,Bash,Shell,我正在使用ProcessBuilder生成命令。我想在以下帖子中构建我的命令: 也就是说,我的命令是这样的: /bin/bash-l-c“我的程序” 但是,我很难将双引号传递到ProcessBuilder,因为newprocessbuilder(List命令)如果我本机将双引号添加到List命令,则无法对该命令进行词组化ProcessBuilder将双引号识别为参数 相关代码: //Construct the argument csi.add("/bin/bash");
ProcessBuilder
生成命令。我想在以下帖子中构建我的命令:
也就是说,我的命令是这样的:
/bin/bash-l-c“我的程序”
但是,我很难将双引号传递到ProcessBuilder
,因为newprocessbuilder(List命令)
如果我本机将双引号添加到List命令
,则无法对该命令进行词组化ProcessBuilder
将双引号识别为参数
相关代码:
//Construct the argument
csi.add("/bin/bash");
csi.add("-l");
csi.add("-c");
csi.add("\"");
csi.add(csi_path);
csi.add(pre_hash);
csi.add(post_hash);
csi.add("\"");
String csi_output = Command.runCommand(project_directory, csi);
public static String runCommand(String directory, List<String> command) {
ProcessBuilder processBuilder = new ProcessBuilder(command).directory(new File(directory));
Process process;
String output = null;
try {
process = processBuilder.start();
//Pause the current thread until the process is done
process.waitFor();
//When the process does not exit properly
if (process.exitValue() != 0) {
//Error
System.out.println("command exited in error: " + process.exitValue());
//Handle the error
return readOutput(process);
}else {
output = readOutput(process);
System.out.println(output);
}
} catch (InterruptedException e) {
System.out.println("Something wrong with command: " +e.getMessage());
} catch (IOException e) {
System.out.println("Something wrong with command: " +e.getMessage());
}
return output;
}
//构造参数
csi.add(“/bin/bash”);
csi.添加(“-l”);
csi.添加(“-c”);
csi.add(“\”);
csi.add(csi_路径);
csi.add(pre_散列);
csi.add(post_散列);
csi.add(“\”);
字符串csi\u output=Command.runCommand(项目目录,csi);
公共静态字符串runCommand(字符串目录,列表命令){
ProcessBuilder ProcessBuilder=newProcessBuilder(命令).directory(新文件(目录));
工艺过程;
字符串输出=null;
试一试{
process=processBuilder.start();
//暂停当前线程,直到进程完成
process.waitFor();
//当进程不能正确退出时
if(process.exitValue()!=0){
//错误
System.out.println(“命令错误退出:+process.exitValue());
//处理错误
返回读取输出(进程);
}否则{
输出=读取输出(过程);
系统输出打印项次(输出);
}
}捕捉(中断异常e){
System.out.println(“命令有问题:+e.getMessage());
}捕获(IOE异常){
System.out.println(“命令有问题:+e.getMessage());
}
返回输出;
}
Ps:我确实想使用ProcessBuilder
而不是Runtime.getRuntime.exec()
,因为我需要在特定目录中运行该命令。我需要使用ProcessBuilder.directory()
Ps:运行后,命令将以2退出。系统似乎可以识别这个命令。奇怪的是,它在使用2退出后没有输出
Ps:预期的命令是
/bin/bash-l-c”/Users/ryouyasachi/GettyGradle/build/idea sandbox/plugins/Getty/classes/python/csi 19f4281 a562db1“
。我打印的值是正确的。解决问题的最佳方法是首先构造命令并将其传递给列表。所以,不要做这些
csi.add("/bin/bash");
csi.add("-l");
csi.add("-c");
csi.add("\"");
csi.add(csi_path);
csi.add(pre_hash);
csi.add(post_hash);
csi.add("\"");
您应该首先构造命令
StringBuilder sb = new StringBuilder();
sb.append("/bin/bash -l -c");
sb.append("\""+csi_path+pre_hash+post_hash+"\"");// add whitespace between the varaible, if required.
System.outprintln(sb.toString()); //verify your command here
csi.add(sb.toString());
此外,请验证以上所有变量值。Thx代表@Ravi的想法
//Construct the argument
csi.add("/bin/bash");
csi.add("-l");
csi.add("-c");
csi.add("\"" + csi_path + " " + pre_hash+ " " + post_hash + "\"");
String csi_output = Command.runCommand(project_directory, csi);
过程
必须分别获取每个参数,以便识别命令。棘手的是,在我想要的命令中
/bin/bash-l-c”/mypath/csi“
“/mypath/csi”
需要通过过程将其视为一个参数
您不需要这样做。列表
中的每个元素都将作为单个参数传递给命令,因此只要将列表
传递给ProcessBuilder
,这就意味着将新的ProcessBuilder(sb.toString())
更改为新的ProcessBuilder(命令)
记住,使用的原因“…”
在命令行上,是对空白进行否定,因此引号之间的所有内容都作为单个参数传递给process@MadProgrammer我试过你的方法,但命令仍然以2退出。问题似乎是添加了/bin/bash-l-c
,因为我的程序在没有它的情况下运行正常。@Jiagiangliang用你最近尝试的方法更新你的帖子。我尝试了你的方法。它似乎无法识别“/bin/bash-l-c”。输出是无法运行程序“/bin/bash-l-c”(在目录中“/Users/ryouyasachi/Getty/semanticiousdifferentials Getty/Getty/dsproj”):错误=2,没有这样的文件或目录,您将pwd
更改为/Users/ryouyasachi/Getty/语义差异Getty/getty/dsproj
。因此,您需要尝试从那里执行/bin/bash-l-c
命令。如果不执行,这不是您的java代码问题。您需要拿出实际的命令并首先手动尝试,然后需要在java代码中进行模拟。不要按相反的顺序操作,否则您将始终面临此问题。我可以从我的终端运行它。变量也很好,我仔细检查了一下。