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 等价物是什


基础教程推荐
- 多个组件的复杂布局 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- Java Swing计时器未清除 2022-01-01