how to run a command at terminal from java program?

前端 未结 6 2162
孤城傲影
孤城傲影 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:07

    As others said, you may run your external program without xterm. However, if you want to run it in a terminal window, e.g. to let the user interact with it, xterm allows you to specify the program to run as parameter.

    xterm -e any command
    

    In Java code this becomes:

    String[] command = { "xterm", "-e", "my", "command", "with", "parameters" };
    Runtime.getRuntime().exec(command);
    

    Or, using ProcessBuilder:

    String[] command = { "xterm", "-e", "my", "command", "with", "parameters" };
    Process proc = new ProcessBuilder(command).start();
    

提交回复
热议问题