问题描述
我已经使用 python 很长时间了.python 的 system 和 subprocess 方法可以使用 shell=True 属性来生成一个设置环境变量的中间进程.在命令运行之前.我一直在使用 Java 来回使用 Runtime.exec() 来执行 shell 命令.
我发现很难在 java 中成功运行一些命令,例如cp -al".我搜索了社区以找到相同的内容,但找不到答案.我只是想确保我在 Java 和 Python 中的调用都以相同的方式运行.
参考
两种可能的方式:
运行时
ProcessBuilder
(推荐)
正如斯蒂芬在评论中指出的那样,为了通过将整个命令作为单个字符串传递来执行构造,设置 command
数组的语法应该是:
<块引用>
Bash 文档
如果存在 -c 选项,则从字符串.
例子:
而且总是有用的*
*可能没用
I've been using python for a long time. python's system and subprocess methods can take shell=True attibute to spawn an intermediate process setting up env vars. before the command runs. I've be using Java back and forth and using Runtime.exec() to execute shell command.
I find difficulty to run some commands in java with success like "cp -al". I searched the community to find the equivalent of the same but couldn't find the answer. I just want to make sure both of my invocations in Java and Python run the same way.
refer
Two possible ways:
Runtime
ProcessBuilder
(recommended)
As Stephen points on the comment, in order to execute constructs by passing the entire command as a single String, syntax to set the command
array should be:
Bash doc
If the -c option is present, then commands are read from string.
Examples:
And the always useful*
*may not be useful
这篇关于Python 的 subprocess shell=True 属性的 Java 等价物是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!