从网站上的 PHP 脚本运行 Java 类文件

发布于 2024-08-19 05:03:07 字数 368 浏览 6 评论 0原文

我有一个网站,希望能够允许用户从该网站在服务器上运行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

心头的小情儿 2024-08-26 05:03:07

PHP exec() 函数是可行的方法,但您应该非常小心您允许执行的内容。换句话说不要依赖用户输入,因为它可能会损害您的整个服务器。

使用 exec 调用 Java 应用程序启动器,您可以从 PHP 执行任何 Java 应用程序,例如

<?php exec("java -jar file.jar arguments", $output); ?>

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 exec("java -jar file.jar arguments", $output); ?>
优雅的叶子 2024-08-26 05:03:07

既然你提到了实时,我建议设置一个 PHP 到 Java 的桥。在每个请求时初始化 JVM 会占用大量资源。

PHP/Java 桥

PHP/Java 桥是一个
流式传输的实现,
基于XML的网络协议,可以
用于连接本机脚本
引擎,例如 PHP、Scheme 或
Python,带有 Java 或 ECMA 335
虚拟机。最多50次
比通过 SOAP 的本地 RPC 更快,
需要更少的资源
网络服务器端。它更快而且
比直接更可靠
通过 Java Native 进行通信
接口,并且不需要
调用 Java 的附加组件
来自 PHP 的过程或 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

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.

半葬歌 2024-08-26 05:03:07

我宁愿将 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

嗳卜坏 2024-08-26 05:03:07

查看 exec 和其他 程序执行函数。但要非常小心地执行此操作,否则就会导致漏洞利用。

Check out exec and the other program execution functions. But do this very carefully, or it's a recipe for exploits.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文