如何在Java应用程序中运行PHP代码? [英] How can I run PHP code within a Java application?

查看:551
本文介绍了如何在Java应用程序中运行PHP代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

从Java调用PHP

我想知道如何运行PHP Java中的代码。使用ScriptEngine,我可以运行JavaScript:

I was wondering how I could run PHP code within Java. Using the ScriptEngine, I am able to run JavaScript:

String code="print(5+5);"; //sample bit of code
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByExtension("js");
try {
    engine.eval(code);
} catch (ScriptException ex) {
    //catch statement
}

为了运行它,我导入了库 javax.script。* 。我相信运行PHP我必须导入一个类似的库,并将上面代码的第三行更改为扩展 php 。不幸的是,我不知道这是哪个库。我用Googled试图找到答案,并且遇到了PHP / Java Bridge库,但我认为这不是我正在寻找的,因为它专注于通过PHP运行Java(据我所知) )。

To run this, I imported the library javax.script.*. I believe to run PHP I would have to import a similar library, and change the third line of the code above to the extension php. Unfortunately, I don't know which library this is. I have Googled to try and find the answer, and came across the PHP/Java Bridge library but I don't think this is exactly what I'm looking for, as it is focussed on running Java through PHP (as far as I know).

我希望我没有错过任何内容,任何帮助都将不胜感激!

I hope I haven't missed anything out, and any help would be greatly appreciated!

推荐答案

此问题的解决方案是下载文件 JavaBridge.jar php-script.jar php-servlet.jar 来自 http://php-java-bridge.sourceforge.net/pjb/download.php 然后将它们导入您的班级:

The solution to this problem is to download the files JavaBridge.jar, php-script.jar and php-servlet.jar from http://php-java-bridge.sourceforge.net/pjb/download.php then import them into your class:

import javax.script.*;

import php.java.bridge.*;
import php.java.script.*;
import php.java.servlet.*;

然后代码可以像以前一样运行:

Then the code can then be run as before:

String code="echo 5+5;"; //sample bit of code
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByExtension("php");
try {
    engine.eval(code);
} catch (ScriptException ex) {
    //catch statement
}

这篇关于如何在Java应用程序中运行PHP代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆