Running Bash commands in Java

后端 未结 7 1131
名媛妹妹
名媛妹妹 2020-11-30 06:03

I have the following class. It allows me to execute commands through java.

public class ExecuteShellCommand {

public String executeCommand(String command) {         


        
相关标签:
7条回答
  • 2020-11-30 06:39

    Each invocation executes in it's own shell. Thus the "cd" of the 2nd invocation is not seen by the third.

    See: https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec(java.lang.String).

    This states that the command is run in a separate process. Thus you have spawned 3 processes.

    If you want all 3 in the same process, try this:

    com.executeCommand("ls; cd bin; ls");
    
    0 讨论(0)
提交回复
热议问题