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

我的未来我决定 提交于 2019-12-31 05:47:05

问题


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 functionality, I thought I would ask if any one knows of a standalone example before I attempted to dig it out of one of those. I glanced at Quartz job scheduling framework also, but I haven't found the UI hooks yet. Do I just need to read from a file with a JSP include?


回答1:


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();
        }
    }
}


来源:https://stackoverflow.com/questions/428901/need-to-execute-shell-script-from-webapp-and-display-console-output-in-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!