从网站上的 PHP 脚本运行 Java 类文件
我有一个网站,希望能够允许用户从该网站在服务器上运行 Java 文件。
我希望用户单击一个按钮,该按钮将在服务器上运行 Java 文件,并且 Java 程序打印到标准输出的任何内容都将打印在网站上供用户查看。
如何做到这一点(从 PHP 调用 Java 程序并将标准从 Java 文件实时反馈回 PHP 网站)?
更新:
感谢您提供有关如何从 PHP 运行 Java 程序的答案。然而,我也希望能够,当 Java 程序打印到标准输出时,它将在执行时打印出大量文本,以便能够在网页上打印出来,以便用户可以看到哪个阶段Java程序正在执行中。
如何做到这一点?是否需要任何额外的 AJAX 或 JavaScript 或类似的东西?
I have a website and want to be able to allow the user to run a Java file on the server from the website.
I want the user to click a button which will run the Java file on the server AND anything printed to standard-out by the Java program will be printed out on the website for the user to see.
How can this be done (call Java program from PHP and feed the standard out from the Java file back to the PHP website in real time)?
Update:
Thanks for the answers on how to run the Java program from PHP. However I also want to be able, as the Java program is printing to stdout where it will be printing out a lot of text as it is executing, to be able to print this out on the webpage so that the user can see what stage the Java program is in its execution.
How can this be done and does it require any additional AJAX or JavaScript or anything like that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
PHP
exec()
函数是可行的方法,但您应该非常小心您允许执行的内容。换句话说不要依赖用户输入,因为它可能会损害您的整个服务器。使用 exec 调用 Java 应用程序启动器,您可以从 PHP 执行任何 Java 应用程序,例如
The PHP
exec()
function is the way to go, but you should be very careful in what you allow to executed.. in other words don't rely on user input as it could potentially compromise your entire server.Calling the Java application launcher using exec, you can execute any Java application from PHP, e.g.
既然你提到了实时,我建议设置一个 PHP 到 Java 的桥。在每个请求时初始化 JVM 会占用大量资源。
PHP/Java 桥
Since you mention real time I would suggest setting up a PHP to Java Bridge. Initializing the JVM at each request takes up a lot of resources.
PHP/Java Bridge
我宁愿将 Java 类包装在 Java 小程序中,然后可以从客户端的 javascript 调用中调用它:请参阅 http://www.rgagnon.com/javadetails/java-0170.html
否则,如果调用向标准输出抛出大量文本或者必须在该类上运行由于系统依赖性,从服务器调用是可行的方法,但您可能需要类似 cometd将文本实时显示在客户端上。有各种 javascript 工具包的实现,例如 Dojo 或 jQuery。
对于服务器端,php此处似乎有一个 cometd 实现。
我希望这有帮助......
菲利普
I would rather wrap the Java class in a Java applet, which can then be invoked from a javascript call on the client side : see http://www.rgagnon.com/javadetails/java-0170.html
Otherwise, if the call throws a lot of text to the standard output or the class has to be run on the server because of system dependencies, calling from php exec is the way to go, but you will probably need something like cometd to display the text on the client in real time. There are implementations for various javascript toolkits such as Dojo or jQuery.
For the server side, there seems to be a cometd implementation in php here.
I hope this helps...
Philippe
查看 exec 和其他 程序执行函数。但要非常小心地执行此操作,否则就会导致漏洞利用。
Check out exec and the other program execution functions. But do this very carefully, or it's a recipe for exploits.
passthru功能有什么用吗?
http://www.php.net/manual/en/function.passthru。 php
Is the passthru function of any use?
http://www.php.net/manual/en/function.passthru.php