how to run a command at terminal from java program?

前端 未结 6 2164
孤城傲影
孤城傲影 2020-11-29 03:14

I need to run a command at terminal in Fedora 16 from a JAVA program. I tried using

Runtime.getRuntime().exec(\"xterm\"); 

but this just op

6条回答
  •  难免孤独
    2020-11-29 04:09

    You need to run it using bash executable like this:

    Runtime.getRuntime().exec("/bin/bash -c your_command");
    

    Update: As suggested by xav, it is advisable to use ProcessBuilder instead:

    String[] args = new String[] {"/bin/bash", "-c", "your_command", "with", "args"};
    Process proc = new ProcessBuilder(args).start();
    

提交回复
热议问题