通过java在linux终端上执行命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4164385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:56:50  来源:igfitidea点击:

executing commands on terminal in linux through java

javalinuxcommandterminalprocessbuilder

提问by Harshit Agarwal

I have created an standalone application in which i want that when the user clicks on the run button then the terminal should open and a particular command should be executed on the terminal. I am able to open the terminal successfully using the following code...

我创建了一个独立的应用程序,我希望当用户单击运行按钮时,终端应该打开,并且应该在终端上执行特定的命令。我可以使用以下代码成功打开终端...

Process process = null;  
try {  
    process = new ProcessBuilder("xterm").start();  
} catch (IOException ex) {  
    System.err.println(ex);  
}  

The above code opens a terminal window but I am not able to execute any command on it. Can anyone tell me how to do that?

上面的代码打开一个终端窗口,但我无法在其上执行任何命令。谁能告诉我怎么做?

采纳答案by Antrromet

Suppose you are trying your gedit command then you need to provide the full qualified path to gedit (e.g /usr/bin/gedit). Similarly for all other command specify the full path.

假设您正在尝试 gedit 命令,那么您需要提供 gedit 的完整路径(例如 /usr/bin/gedit)。同样,对于所有其他命令,请指定完整路径。

回答by Kilian Foth

Try

尝试

new ProcessBuilder("xterm", "-e", 
                   "/full/path/to/your/program").start()

回答by ivanceras

Execute any command in linux as is,as what you type in the terminal:

在 linux 中按原样执行任何命令,就像您在终端中键入的一样:

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;

    public class CommandExecutor {
    public static String execute(String command){
        StringBuilder sb = new StringBuilder();
        String[] commands = new String[]{"/bin/sh","-c", command};
        try {
            Process proc = new ProcessBuilder(commands).start();
            BufferedReader stdInput = new BufferedReader(new 
                    InputStreamReader(proc.getInputStream()));

            BufferedReader stdError = new BufferedReader(new 
                    InputStreamReader(proc.getErrorStream()));

            String s = null;
            while ((s = stdInput.readLine()) != null) {
                sb.append(s);
                sb.append("\n");
            }

            while ((s = stdError.readLine()) != null) {
                sb.append(s);
                sb.append("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

}

Usage:

用法:

CommandExecutor.execute("ps ax | grep postgres");

or as complex as:

或者像这样复杂:

CommandExecutor.execute("echo 'hello world' | openssl rsautl -encrypt -inkey public.pem -pubin | openssl enc -base64");

String command = "ssh user@database-dev 'pg_dump -U postgres -w -h localhost db1 --schema-only'";
CommandExecutor.execute(command);