Python 的 subprocess shell=True 属性的 Java 等价物是什么?

what is the Java equivalent of Pythons#39;s subprocess shell=True property?(Python 的 subprocess shell=True 属性的 Java 等价物是什么?)
本文介绍了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 中的调用都以相同的方式运行.

参考

解决方案

两种可能的方式:

  1. 运行时

     String[] command = {sh", cp", -al"};进程 shellP = Runtime.getRuntime().exec(command);

  2. 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:

  1. Runtime

     String[] command = {"sh", "cp", "-al"};
     Process shellP = Runtime.getRuntime().exec(command);
    

  2. 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 等价物是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Show padding zeros using DecimalFormat(使用 DecimalFormat 显示填充零)
Java decimal formatting using String.format?(使用 String.format 的 Java 十进制格式?)
Sprintf equivalent in Java(Java 中的 Sprintf 等价物)
StAX XML formatting in Java(Java 中的 StAX XML 格式)
Java - format double value as dollar amount(Java - 将双值格式化为美元金额)
Java printf using variable field size?(Java printf使用可变字段大小?)