我可以获得Java Runtime.getRuntime().exec的真正完整执行命令吗?
当我试图用Java运行我可以获得Java Runtime.getRuntime().exec的真正完整执行命令吗?,java,linux,ansible,Java,Linux,Ansible,当我试图用Java运行Runtime.getRuntime().exec运行Ansible时 以下是我所做的: String[] cmd = {"ansible-playbook", "/path/to/playbook", "--extra-vars", "'{\"filePath\":\"/path/to/file\"}'"}; Process process = Runtime.getRuntime().exec(cmd, null); 我收到如下错误消息: FAILED! =>
Runtime.getRuntime().exec
运行Ansible时
以下是我所做的:
String[] cmd = {"ansible-playbook", "/path/to/playbook", "--extra-vars", "'{\"filePath\":\"/path/to/file\"}'"};
Process process = Runtime.getRuntime().exec(cmd, null);
我收到如下错误消息:
FAILED! => {"failed": true, "msg": "'filePath' is undefined"}
但是,当我对终端执行相同的命令时:
ansible-playbook /path/to/playbook --extra-vars '{"filePath":"/path/to/file"}'
一切都很好
我想我在terminal和Java中运行的命令之间一定有一些区别,可能是撇号或引号
我想知道是否有任何方法可以获得真正执行的
Runtime.getRuntime().exec
?就像我可以通过history
..获取某个用户的命令行历史一样,您正在第三个参数中添加额外的引号:
"'{\"filePath\":\"/path/to/file\"}'"
如果这样做,您就不会在shell中执行与上面相同的命令。您实际上正在执行(在bash中):
这里的值不需要单引号:因为您直接传递这些值,所以不必担心在shell中必须进行的引号。您可以简单地使用:
"{\"filePath\":\"/path/to/file\"}"
您不需要在第三个参数中使用单引号:exec命令为您“引号”它<代码>“{\“filePath\”:\“/path/to/file\”}”很好。@AndyTurner谢谢!为什么单引号版本在终端上工作…因为终端不为您报价。
"{\"filePath\":\"/path/to/file\"}"