How to invoke a Linux shell command from Java

前端 未结 3 968
轻奢々
轻奢々 2020-11-22 13:52

I am trying to execute some Linux commands from Java using redirection (>&) and pipes (|). How can Java invoke csh or bash commands?

I

3条回答
  •  萌比男神i
    2020-11-22 14:43

    exec does not execute a command in your shell

    try

    Process p = Runtime.getRuntime().exec(new String[]{"csh","-c","cat /home/narek/pk.txt"});
    

    instead.

    EDIT:: I don't have csh on my system so I used bash instead. The following worked for me

    Process p = Runtime.getRuntime().exec(new String[]{"bash","-c","ls /home/XXX"});
    

提交回复
热议问题