Need to execute shell script from webapp and display console output in page

前端 未结 1 1375
清歌不尽
清歌不尽 2021-01-28 13:44

I am looking for java examples or library that will help me integrate this into a Struts2/Spring application. Many build systems such as Luntbuild or Hudson have this functiona

1条回答
  •  离开以前
    2021-01-28 14:18

    If I understand what you are trying to do, this is one way in Java. This executes the ls command and then captures the shell output and writes it back to System.out.

    public class TestShell {
        public static void main(String[] args)
        {
            try
            {
                    String cmd = "ls\n";
                    Process p = Runtime.getRuntime().exec(cmd);
                    p.waitFor();
                    BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
                    while(r.ready()) {
                        System.out.println(r.readLine());
                    }
    
            }
            catch (Throwable t)
                    {
                    t.printStackTrace();
            }
        }
    }
    

    0 讨论(0)
提交回复
热议问题