【问题标题】:Java and Unix corroboration [duplicate]Java和Unix确证[重复]
【发布时间】:2011-05-31 23:46:51
【问题描述】:

可能重复:
How to run Unix shell script from java code?

我正在使用 spring mvc 创建一个 Web 应用程序,它将是多用户应用程序。每个用户将创建自己的配置等。

完成所有配置后,他们应该开始从 Web 应用程序构建和运行他们的项目(从 java 执行 shell 脚本),今天我在谷歌搜索时偶然发现了这篇文章

How to run Unix shell script from Java code?

您对此有何看法,除了Runtime.getRuntime(),还有没有更好的方法...

【问题讨论】:

  • How to run Unix shell script from java code? 的可能重复项考虑使用最流行的答案,这在您的情况下是一个很好的答案。而且,如果你有典型的跨平台需求,我建议你走蚂蚁的路。

标签: java


【解决方案1】:

更好一点的方法是使用提供更方便 API 的 ProcessBuilder 类。

【讨论】:

    【解决方案2】:

    1.

    有没有比 Runtime.getRuntime() 更好的方法来做到这一点

    “更好”是什么意思?

    2.

    Runtime.getRuntime().exec(myCommand);
    

    这很好用,但请记住,在这种情况下不会执行 .bashrc(或类似的环境设置)。此外,您不会捕获输出。

    3。 只是作为一个辅助说明。这是我的 shell 界面:

    import java.io.*;
    
    public class Shell
    {
        Process proc;
        BufferedReader in;
        BufferedReader err;
        PrintWriter out;
    
        public Shell() throws IOException
        {
           proc = Runtime.getRuntime().exec("/bin/bash");
           in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
           err = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
           out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
    
           exec("source ~/.bashrc");
        }
    
    
        public void exec(String cmd)
        {
            out.println(cmd);
    
            try {
                while (in.ready())
                    System.out.println(in.readLine());
    
                while (err.ready())
                    System.err.println(err.readLine());
            }
            catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    
        public void close()
        {
            try {
                out.println("exit");
                proc.waitFor();
    
                while (in.ready())
                    System.out.println(in.readLine());
    
                while (err.ready())
                    System.err.println(err.readLine());
    
                in.close();
                out.close();
                proc.destroy();
            } 
            catch (IOException e) {
                e.printStackTrace();
            } 
            catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
        public static void execute(String command) throws IOException
        {
            System.out.println("Executing: " + command);
            Shell shell = new Shell();
            shell.exec(command);
            shell.close();
        }
    
        public static void main(String[] args) throws IOException
        {   
            Shell.execute("ls -l ~");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2010-11-05
      • 2013-10-27
      • 2017-01-10
      • 2011-04-24
      • 2013-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-20
      相关资源
      最近更新 更多