Executing shell-script with parameters from java

前端 未结 3 847
一生所求
一生所求 2021-02-14 07:48

I have been googling for some time, and everyone seems to have a different solution, none of which appear to be working for me.

I have tried both ProcessBuilder

3条回答
  •  囚心锁ツ
    2021-02-14 08:13

    One option is to handle ~ yourself:

    String homeDir = System.getenv("HOME");
    String[] cmd = { homeDir + "/path/to/shellscript.sh", "foo", "bar" };
    Process p = Runtime.getRuntime().exec(cmd);
    

    Another is to let Bash handle it for you:

    String[] cmd = { "bash", "-c", "~/path/to/shellscript.sh foo bar" };
    Process p = Runtime.getRuntime().exec(cmd);
    

提交回复
热议问题