Tomcat无法执行外部Java程序 [英] Tomcat fails to execute external java program
问题描述
我是JSPs/Tomcat的新手,并且在Java方面也是一个新手.这就是我要做的-当用户单击按钮/访问URL时,我想启动一个Java程序(带有一些命令行参数).
I am a total newbie in JSPs/Tomcat and to a large extent in Java as well. Here's what I have to do -- when a user clicks a button/visits a URL, I want to launch a Java program (which takes some command line arguments).
我可以很容易地完成
Runtime.exec("C:\\Python27\\python.exe test.py")
或
Runtime.exec("java -cp %CLASSPATH%;C:\\devprojects HelloWorld"
,这很好. HelloWorld.class只是在其中打印"HelloWorld"的地方.
and this works fine. Where HelloWorld.class just prints "HelloWorld".
但是,当我尝试使用带有命令行参数的Java程序时,GET请求只是挂起而没有执行任何操作.我不知道要查找什么日志,或者这里可能出了什么问题.在花了两天时间尝试各种事情之后,我现在要放弃了.
However, when I attempt a java program which takes command line arguments, the GET request just hangs doing nothing. I don't know what logs to look for or what could be wrong here. After having spent TWO days on trying various things, I am just about to give up now.
Runtime.exec("java -cp %CLASSPATH%;C:\\devprojects Run --username Blah --password Foo");
Tomcat最终以哪个用户身份运行此Java程序?我可以成为管理员吗?这是在Windows 2008上,UAC是否会干扰事物?
What user does Tomcat end up running this java program as? Can I make it to be Administrator? This is on Windows 2008, does UAC interfere with things?
我不能在这里修改Run.class,我必须按原样并使用命令行参数来运行它.
I cannot modify the Run.class here, I HAVE to run it as is and with command line parameters.
请告知.
推荐答案
一个想法:您将命令行的默认标记化为一个完整的String
,并且它没有像您期望的那样解析最后一个.相反,您应该在自己切掉命令行之后使用采用String[]
的这种方法的形式:
One idea: you are relying on the default tokenization of your command line as one complete String
, and it is not parsing the last one as you expect. Instead you should use the form of this method that takes a String[]
, after you have chopped up the command line yourself:
http://docs.oracle.com/javase/6/docs/api/java/lang/Runtime.html#exec(java.lang.String [])
或者,它正在等待您的输入,或者正在等待您读取其输出.这可以解释挂起.在互联网上搜索流和Runtime.exec()
的危险.
Or, it is waiting for input from you, or waiting for you to read its output. This could explain the hang. Search the internet for the dangers of streams and Runtime.exec()
.
请考虑ProcessBuilder
.
这篇关于Tomcat无法执行外部Java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!