从网站上的 PHP 脚本运行 Java 类文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2128619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run Java class file from PHP script on a website
提问by tree-hacker
I have a website and want to be able to allow the user to run a Java file on the server from the website.
我有一个网站,并希望能够允许用户从该网站在服务器上运行 Java 文件。
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.
我希望用户单击一个按钮,该按钮将在服务器上运行 Java 文件,并且 Java 程序打印到标准输出的任何内容都将打印在网站上供用户查看。
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)?
如何做到这一点(从 PHP 调用 Java 程序并将 Java 文件中的标准实时反馈给 PHP 网站)?
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.
感谢您提供有关如何从 PHP 运行 Java 程序的答案。但是我也希望能够,因为 Java 程序正在打印到标准输出,它将在执行时打印出大量文本,以便能够在网页上打印出来,以便用户可以看到哪个阶段Java 程序正在执行中。
How can this be done and does it require any additional AJAX or JavaScript or anything like that?
如何做到这一点,它是否需要任何额外的 AJAX 或 JavaScript 或类似的东西?
采纳答案by James Goodwin
The PHP exec()
function is the way to go, but you should be verycarefulin what you allow to executed.. in other words don't rely on user input as it could potentially compromise your entire server.
PHPexec()
函数是可行的方法,但您应该非常小心允许执行的内容。换句话说,不要依赖用户输入,因为它可能会危及您的整个服务器。
Calling the Java application launcher using exec, you can execute any Java application from PHP, e.g.
使用 exec 调用 Java 应用程序启动器,您可以从 PHP 执行任何 Java 应用程序,例如
<?php exec("java -jar file.jar arguments", $output); ?>
回答by Bobby Hyman
Check out execand the other program execution functions. But do this verycarefully, or it's a recipe for exploits.
回答by brianjd
Is the passthru function of any use?
passthru 功能有什么用吗?
回答by Peter Lindqvist
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。在每次请求时初始化 JVM 会占用大量资源。
The PHP/Java Bridge is an implementation of a streaming, XML-based network protocol, which can be used to connect a native script engine, for example PHP, Scheme or Python, with a Java or ECMA 335 virtual machine. It is up to 50 times faster than local RPC via SOAP, requires less resources on the web-server side. It is faster and more reliable than direct communication via the Java Native Interface, and it requires no additional components to invoke Java procedures from PHP or PHP procedures from Java.
PHP/Java Bridge 是一种基于 XML 的流式网络协议的实现,可用于将本机脚本引擎(例如 PHP、Scheme 或 Python)与 Java 或 ECMA 335 虚拟机连接起来。它比通过 SOAP 的本地 RPC 快 50 倍,在 Web 服务器端需要更少的资源。它比通过 Java Native Interface 的直接通信更快、更可靠,并且不需要额外的组件来从 PHP 调用 Java 过程或从 Java 调用 PHP 过程。
回答by Philippe
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
我宁愿将 Java 类包装在 Java 小程序中,然后可以从客户端的 javascript 调用中调用它:请参阅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 cometdto display the text on the client in real time. There are implementations for various javascript toolkits such as Dojo or jQuery.
否则,如果调用向标准输出抛出大量文本,或者由于系统依赖性而必须在服务器上运行该类,则从 php exec 调用是可行的方法,但您可能需要像cometd这样的东西来显示客户端上的实时文本。有各种 javascript 工具包的实现,例如 Dojo 或 jQuery。
For the server side, there seems to be a cometd implementation in php here.
对于服务器端,php here 中似乎有一个cometd 实现。
I hope this helps...
我希望这有帮助...
Philippe
菲利普