Pregunta

I am trying to run some PHP snippets from my Java application.

I have seen this question: PHP Call from Java Using Quercus

which suggests simply:

import com.caucho.quercus.QuercusEngine;

QuercusEngine engine = new QuercusEngine();
engine.setOutputStream(System.out);
engine.executeFile("src/test.php");

and I have seen http://wiki.caucho.com/Quercus%3a_Command_Line_Interface_%28CLI%29 which indicates:

QuercusEngine engine = new QuercusEngine();
engine.setIni("foo", "bar");
engine.execute("<?php var_dump(ini_get('foo')); ?>");

But on doing that, I get:

Exception in thread "main" com.caucho.quercus.QuercusErrorException: eval::1: Fatal Error: 'var_dump' is an unknown function.
        at com.caucho.quercus.env.Env.error(Env.java:6559)
        at com.caucho.quercus.env.Env.error(Env.java:6445)
        at com.caucho.quercus.env.Env.error(Env.java:6109)
        at com.caucho.quercus.expr.CallExpr.evalImpl(CallExpr.java:198)
        at com.caucho.quercus.expr.CallExpr.eval(CallExpr.java:151)
        at com.caucho.quercus.expr.Expr.evalTop(Expr.java:523)
        at com.caucho.quercus.statement.ExprStatement.execute(ExprStatement.java:67)
        at com.caucho.quercus.program.QuercusProgram.execute(QuercusProgram.java:413)
        at com.caucho.quercus.QuercusEngine.execute(QuercusEngine.java:139)
        at com.caucho.quercus.QuercusEngine.execute(QuercusEngine.java:100)

I get the same thing for any function -- strlen, strcmp, phpinfo, etc.

As described on http://quercus.caucho.com/ ...

The Quercus .war file can be run on Java application servers such as Glassfish, i.e. it can be run outside of Resin. This .war file includes the Quercus interpreter and the PHP libraries.

I have downloaded the war file from that page (http://caucho.com/download/quercus-4.0.25.war)

and am running

$ javac -cp 'WEB-INF/lib/resin.jar;.' TestQuercus.java
$ java -cp 'WEB-INF/lib/resin.jar;.' TestQuercus

the complete file is

import com.caucho.quercus.QuercusEngine;

public class TestQuercus {

    public static void main(String[] args) throws Exception {
    QuercusEngine engine = new QuercusEngine();
    engine.setIni("foo", "bar");
    engine.execute("<?php var_dump(ini_get('foo')); ?>");
    }

}

I am able to do

java -cp 'WEB-INF/lib/resin.jar' com.caucho.quercus.CliQuercus foo.php

where foo.php includes <?php phpinfo; ?>

So how can I get from there to a working QuercusEngine?

Thanks,

¿Fue útil?

Solución

Works as below.

import javax.script.ScriptEngine;
import com.caucho.quercus.script.QuercusScriptEngineFactory;

QuercusScriptEngineFactory factory = new QuercusScriptEngineFactory();
ScriptEngine engine = factory.getScriptEngine();

String code = "<?php $foo = strlen('abc'); print $foo; return 'yikes'; ?>";
Object o = engine.eval(code);
System.out.println(o);

Otros consejos

This was a bug. I just fixed it for 4.0.33, which should be out pretty soon. If you like, you can build Quercus yourself from our public svn repository:

svn checkout svn://svn.caucho.com/home/svn/svnroot/resin/trunk resin
cd resin
ant

Jars will be resin/lib/resin-kernel.jar and resin/lib/quercus.jar.

For bug reference, see http://bugs.caucho.com/view.php?id=5270.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top