从Web应用程序执行外部Java程序 [英] Executing external Java program from a webapp

查看:156
本文介绍了从Web应用程序执行外部Java程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从在Tomcat 6,Windows 7、64bit上运行的servlet调用外部Java类文件. 关于这个主题已经有一些话题,但是没有一个真正对我有帮助.

I am trying to call an external Java class file from a servlet running on Tomcat 6, Windows 7, 64bit . There are already some threads on this subject around, but none are really helping me.

Fyi,如果我直接从shell运行它,我就能够成功做到这一点.

Fyi, I have successfully been able to do this if I run it from the shell directly.

我使用ProcessBuilder执行这样的命令

 ProcessBuilder bp = new ProcessBuilder("cmd.exe","/C","java", "TheExternalClass", "ParameterA" });

我还使用了创建的Process中的errorStream和inputStream.

I'm also consuming the errorStream and inputStream from the created Process.

当我从servlet运行它时,它只是停顿了.

When I run it from the servlet it simply stalls.

例如,如果我用java命令将dir设置为dir,它的确可以按预期运行,这使我相信它与内存有关,或者与从Tomcat启动新的Java Process或类似问题有关.

If I for example swith the java command to dir it does work as expected, leading me to believe it has something to do with either memory, or issues starting up a new Java Process from Tomcat or something like that.

有人对如何解决这个问题有一个指示或好主意吗?

Anybody has a pointer or a good idea on how to solve this?

有关该主题的其他一些帖子:

Some other posts on the topic:

http://www.javaworld .com/jw-12-2000/jw-1229-traps.html?page = 1

Tomcat无法执行外部Java程序

非常感谢您阅读.

推荐答案

上面的代码有效,并且不会使servlet停顿.我的猜测是您稍后调用p.waitFor()或类似的命令,或者您在JSP线程中读取了该进程的输出流-并且 将阻塞.

The code above works and it doesn't stall your servlet. My guess is that you call p.waitFor() or similar later or that you read the output streams of the process in the JSP thread - and that will block.

如果您不想阻止,则有两个选择:

If you don't want to block, you have two options:

  • 使用AJAX在后台轮询JSP. JSP仍将阻止,但浏览器将可用.
  • 创建一个读取输出流的后台线程.这将使JSP立即返回,但是您将不得不找到一种将处理结果发送到浏览器的方法,因为它不知道服务器上会发生什么.

这篇关于从Web应用程序执行外部Java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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