|
From: <php...@li...> - 2008-09-22 13:56:39
|
Dear all,
I am new to the PHP/Java bridge and trying to run the standard examples. I'm
trying to connect Java 1.6.0.6 with PHP 5.2.2 to call PHP from JAVA using
the scripting engine. The installation itself via the copy statement was
fine. I tried two options, in both cases the code compiles and seems to
connect to the PHP engine,but the results are not as hoped for:
// OPTION 1
javax.script.ScriptEngineManager manager = new
javax.script.ScriptEngineManager();
javax.script.ScriptEngine e = manager.getEngineByName("php-invocable");
e.eval("<?php class f {function a($p) {return java_values($p)+1;}}\n" +
"java_context()->setAttribute('f', java_closure(new f()), 100); ?>");
javax.script.Invocable i = (javax.script.Invocable)e;
Object f = e.getContext().getAttribute("f", 100);
System.out.println("Invoking");
String res = (String) i.invokeMethod(f, "a", new Object[] {new Integer(1)});
Sytem.out.println("Output: " + res);
e.eval ((java.io.Reader) null);
This leads to a null pointer exception after printing the line 'invoking'
when trying to call invokeMethod. That is, locating the 'f' function does
not lead to an object.
OPTION 2
System.out.println("Interactive engine");
javax.script.ScriptEngine e2 = (new
javax.script.ScriptEngineManager()).getEngineByName("php-interactive");
e2.eval("$v = 1+2");
System.out.println(e2.eval("echo $v"));
e2.eval((String)null);
This leads to a stack overflow exception:
Exception in thread "main" java.lang.StackOverflowError
at
javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:247)
at
php.java.script.InvocablePhpScriptEngine.invoke(InvocablePhpScriptEngine.java:97)
at
php.java.script.InvocablePhpScriptEngine.invokeFunction(InvocablePhpScriptEngine.java:108)
at
php.java.script.InteractivePhpScriptEngine.eval(InteractivePhpScriptEngine.java:91)
at
javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:247)
at
php.java.script.InvocablePhpScriptEngine.invoke(InvocablePhpScriptEngine.java:97)
at
php.java.script.InvocablePhpScriptEngine.invokeFunction(InvocablePhpScriptEngine.java:108)
at
php.java.script.InteractivePhpScriptEngine.eval(InteractivePhpScriptEngine.java:91)
at
javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:247)
at
php.java.script.InvocablePhpScriptEngine.invoke(InvocablePhpScriptEngine.java:97)
and etceteras
Any suggestions here on what I'm doing wrong?
Thanks already for any suggestions you can provide!
Kind regards,
Bart Orriens
The new is always the old. Everything that's old was once new, and
everything that's new will one day be old. The oldest thing of all is the
present, because there's never been anything else but the present. No one
has ever lived in the past, and no one lives in the future, either.
|