需要从webapp执行shell脚本并在页面中显示控制台输出 [英] Need to execute shell script from webapp and display console output in page

查看:215
本文介绍了需要从webapp执行shell脚本并在页面中显示控制台输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找可以帮助我将其集成到Struts2 / Spring应用程序中的java示例或库。许多构建系统,如Luntbuild或Hudson都有这个功能,我想我会问,如果有人知道一个独立的例子,我试图从其中一个中挖掘它。我也看了一下Quartz作业调度框架,但我还没有找到UI挂钩。我只需要从包含JSP的文件中读取吗?

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?

推荐答案

如果我理解你要做什么,这个是Java中的一种方式。这将执行ls命令,然后捕获shell输出并将其写回System.out。

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

这篇关于需要从webapp执行shell脚本并在页面中显示控制台输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆