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

时间:2021-04-09 00:03:14

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程序正在打印到stdout,它将在执行时打印出大量文本,以便能够在网页上打印出来,以便用户可以看到Java程序正在执行中。

How can this be done and does it require any additional AJAX or JavaScript or anything like that?

如何做到这一点,是否需要任何额外的AJAX或JavaScript或类似的东西?

5 个解决方案

#1


45  

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.

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

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); ?>

#2


14  

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会占用大量资源。

PHP/Java Bridge

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.

PHP / Java Bridge是基于XML的流式网络协议的实现,可用于将本机脚本引擎(例如PHP,Scheme或Python)与Java或ECMA 335虚拟机连接。它通过SOAP比本地RPC快50倍,在Web服务器端需要的资源更少。它比通过Java Native Interface的直接通信更快,更可靠,并且它不需要额外的组件来从Java调用Java程序或PHP程序。

#3


4  

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

查看exec和其他程序执行函数。但要非常小心地这样做,否则它就是一个利用漏洞的方法。

#4


4  

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 applet中,然后可以从客户端的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 cometd to 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似乎有一个cometd实现。

I hope this helps...

我希望这有帮助...

Philippe

菲利普

#5


1  

Is the passthru function of any use?

passthru功能有用吗?

http://www.php.net/manual/en/function.passthru.php

http://www.php.net/manual/en/function.passthru.php

#1


45  

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.

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

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); ?>

#2


14  

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会占用大量资源。

PHP/Java Bridge

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.

PHP / Java Bridge是基于XML的流式网络协议的实现,可用于将本机脚本引擎(例如PHP,Scheme或Python)与Java或ECMA 335虚拟机连接。它通过SOAP比本地RPC快50倍,在Web服务器端需要的资源更少。它比通过Java Native Interface的直接通信更快,更可靠,并且它不需要额外的组件来从Java调用Java程序或PHP程序。

#3


4  

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

查看exec和其他程序执行函数。但要非常小心地这样做,否则它就是一个利用漏洞的方法。

#4


4  

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 applet中,然后可以从客户端的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 cometd to 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似乎有一个cometd实现。

I hope this helps...

我希望这有帮助...

Philippe

菲利普

#5


1  

Is the passthru function of any use?

passthru功能有用吗?

http://www.php.net/manual/en/function.passthru.php

http://www.php.net/manual/en/function.passthru.php