Skip to content
Advertisement

what is the Java equivalent of Pythons’s subprocess shell=True property?

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.

JavaScript

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

Advertisement

Answer

Two possible ways:

  1. Runtime

    JavaScript
  2. ProcessBuilder (recommended)

    JavaScript

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:

JavaScript

Bash doc

If the -c option is present, then commands are read from string.

Examples:

JavaScript

And the always useful*

JavaScript

*may not be useful

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement