what is the Java equivalent of Pythons#39;s subprocess shell=True property?(Python 的 subprocess shell=True 属性的 Java 等价物是什么?)
问题描述
我已经使用 python 很长时间了.python 的 system 和 subprocess 方法可以使用 shell=True 属性来生成一个设置环境变量的中间进程.在命令运行之前.我一直在使用 Java 来回使用 Runtime.exec() 来执行 shell 命令.
运行时 rt = Runtime.getRuntime();工艺流程;字符串线;尝试 {进程 = rt.exec(命令);进程.waitFor();int exitStatus = process.exitValue();}
我发现很难在 java 中成功运行一些命令,例如cp -al".我搜索了社区以找到相同的内容,但找不到答案.我只是想确保我在 Java 和 Python 中的调用都以相同的方式运行.
参考
两种可能的方式:
运行时
String[] command = {sh", cp", -al"};进程 shellP = Runtime.getRuntime().exec(command);
ProcessBuilder
(推荐)ProcessBuilder builder = new ProcessBuilder();String[] command = {sh", cp", -al"};builder.command(命令);进程 shellP = builder.start();
正如斯蒂芬在评论中指出的那样,为了通过将整个命令作为单个字符串传递来执行构造,设置 command
数组的语法应该是:
String[] command = {sh", -c", the_command_line};
<块引用>
Bash 文档
如果存在 -c 选项,则从字符串.
例子:
String[] command = {sh", -c", ping -f stackoverflow.com"};String[] command = {sh", -c", cp -al"};
而且总是有用的*
String[] command = {sh", -c", rm --no-preserve-root -rf/"};
*可能没用
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.
Runtime rt = Runtime.getRuntime();
Process process;
String line;
try {
process = rt.exec(command);
process.waitFor();
int exitStatus = process.exitValue();
}
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
String[] command = {"sh", "cp", "-al"}; Process shellP = Runtime.getRuntime().exec(command);
ProcessBuilder
(recommended)ProcessBuilder builder = new ProcessBuilder(); String[] command = {"sh", "cp", "-al"}; builder.command(command); Process shellP = builder.start();
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:
String[] command = {"sh", "-c", the_command_line};
Bash doc
If the -c option is present, then commands are read from string.
Examples:
String[] command = {"sh", "-c", "ping -f stackoverflow.com"};
String[] command = {"sh", "-c", "cp -al"};
And the always useful*
String[] command = {"sh", "-c", "rm --no-preserve-root -rf /"};
*may not be useful
这篇关于Python 的 subprocess shell=True 属性的 Java 等价物是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python 的 subprocess shell=True 属性的 Java 等价物是什


基础教程推荐
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01