Eclipse Wiki Weblog

All | General | Java | Eclipse | Groovy | Grails | GWT | Google | MathEclipse | Bliki
20070319 Monday March 19, 2007

Experiments with the JSR-223 compatible Quercus PHP engine

There's a lot of hype for all sorts of Java scripting engines at the moment. For myself I've made some experiments with groovy/grails. Today I've found the PHP Quercus engine from Caucho.com, which is licensed under GPL and I also tried to use it as a JSR-223 scripting engine. As the goal of the test, I want to generate some text from a PHP file, which prints out some PHP variables ($x, $y) in a PHP double quoted string.

Therefore I opened the downloadable *.war file from the Quercus 3.1 snapshot (alpha) with my ZIP tool and extracted:

in the /lib classpath of a newly created quercus.script.test Eclipse project.

Additionally I added a servlet-api.jar from a tomcat 6.x installation to the project's classpath.

The Quercus standard JSR-223 script engine is implemented in the package com.caucho.quercus.script.

Note: if you're not using JDK 1.6, but only JDK 1.5, you'll also need the javax.scripting.* JSR 223 (see link at the end of the blog entry). I also didn't install necessary additional *.jar libraries for Quercus MailModule and UnicodeModule classes.

The Test.java file below now creates the following output

hello world
Hello - World
Hasta la vista Baby

Test.java file (updated: it now reflects the comments from baennaeck)

package info.bliki.quercus.script.test;

import java.io.FileReader;
import java.io.StringWriter;

import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class Test {

	public static void main(String[] args) {
		ScriptEngineManager scriptManager = new ScriptEngineManager();

		Object php2javaResult = null;
		ScriptEngine phpEngine = scriptManager.getEngineByExtension("php");
		ScriptContext context = phpEngine.getContext();
		
		try {
			context.setWriter(new StringWriter());
			php2javaResult = phpEngine.eval("<?php echo \"hello world\"; ?>",
					context);
			StringWriter writer = (StringWriter) context.getWriter();
                        // show output from PHP script in console:
		        System.out.println(writer.toString());
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		try {
			context.setWriter(new StringWriter());
			phpEngine.put("x", "Hello");
			phpEngine.put("y", "World");
			php2javaResult = phpEngine.eval("<?php echo \"$x - $y\"; ?>", context);
			StringWriter writer = (StringWriter) context.getWriter();
			System.out.println(writer.toString());
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		try {// file test
			context.setWriter(new StringWriter());
			//redefine variable in ENGINE_SCOPE x,y:
			phpEngine.put("x", "Hasta la vista");
			phpEngine.put("y", "Baby");
			// the following file contains the line: <?php echo "$x $y"; ?>
			php2javaResult = phpEngine
					.eval(new FileReader(
							"C:\\temp\\echo_test.php"), context);
			StringWriter writer = (StringWriter) context.getWriter();
			System.out.println(writer.toString());
		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}
}

BTW: the Quercus phpinfo() implementation gives the following output:

<h1>Quercus</h1><pre>PHP Version => 5.2.0
System => Windows XP 5.1 x86
Build Date => 20061220T1222
Configure Command => n/a
Server API => CGI
Virtual Directory Support => disabled
Configuration File (php.ini) Path => WEB-INF/php.ini
PHP API => 20031224
PHP Extension => 20041030
Debug Build => no
Thread Safety => enabled
Registered PHP Streams => php, file, http, https
</pre>

Related links:

Posted by axelclk ( Mar 19 2007, 10:58:47 PM CET ) Permalink Comments [14]

Trackback URL: http://www.groovy-news.org/e/trackback/axelclk/Weblog/experiments_with_the_jsr_223
Linkbacks:
Comments:

Hi!

Your blog helped me a lot to finally get quercus work. Unfortunately I don't know why you've modified the quercus eval method. For me the following code works out-of-the-box without any modifications:

Number num = new Number();
num.setNumber(42);
ScriptEngine engine = new ScriptEngineManager().getEngineByExtension("php");
ScriptContext ctx = engine.getContext();
StringWriter writer = new StringWriter();
ctx.setWriter(writer);
engine.put("num", num);
try {
engine.eval("<? echo $num->getNumber(); ?>");
} catch (ScriptException e) {
e.printStackTrace();
}
System.out.println(writer);

I think the problem is, that you create a new context and overwrite the existing context by passing it to the eval method.

Posted by baennaeck (155.56.68.221) on April 03, 2007 at 11:45 AM CEST #

Seems that you're right.
I think I've experimetned so much, that I've overseen this detail.
I will change the above article.

Posted by 87.181.245.121 on April 07, 2007 at 05:16 PM CEST #

Thank you for this code but what have i do for calling a PHP page t've tried this :

phpEngine.eval("<?php < a href=\"http://localhost/teste.php\">; ?>",context);

but it make me a message error :

com.caucho.quercus.parser.QuercusParseException: eval::1: href is an invalid left-hand side of an assignment.
at com.caucho.quercus.parser.QuercusParser.error(QuercusParser.java:4681)

and i've tried this too :
Invocable invocableEngine = (Invocable) phpEngine
but it doesn't accepted this???

thank you for helping us...

Posted by kenza_sana (213.154.92.220) on May 04, 2007 at 03:16 PM CEST #

Your given string isn't valid PHP code.

Try something like this if you want to print the HTML:

phpEngine.eval("<?php echo '< a href=\"http://localhost/teste.php\">;' ?>",context);

or use the FileReader example at the end of the test to read in your <code>../teste.php</code> file from local file system.

Posted by axelclk (87.181.201.248) on May 04, 2007 at 05:34 PM CEST #

Thank you i'v tested it with the filereader to execute a php fonction :

****** Test.java *****
ScriptEngineManager scriptManager = new ScriptEngineManager();

Object php2javaResult = null;
ScriptEngine phpEngine = scriptManager.getEngineByExtension("php");
ScriptContext context = phpEngine.getContext();

context.setWriter(new StringWriter());
php2javaResult = phpEngine.eval(new FileReader("C:\\Test.php"),context);
Invocable inv = (Invocable) phpEngine;
inv.invokeFunction("testMessage", "Salut " );

***************** Test.php ************
<?php
function testMessage(msg)
{
print("Printing : " + msg+" ");
}

?>
*********************************************
But it give me this error message :
"java.lang.ClassCastException: com.caucho.quercus.script.QuercusScriptEngine
at info.bliki.quercus.script.test.Test2.main"
at line : Invocable inv = (Invocable) phpEngine;

Thank you for you answer...

Posted by kenza_sana (213.154.92.220) on May 04, 2007 at 06:15 PM CEST #

Great article, however try as a I might, I just can't get it to work on Java 5 -- required as Apple haven't a production ready Java 6 release.

I've downloaded the JSR 223's reference implementation and tried using the script.jar and js.jar from there, however I get a "java.lang.UnsupportedClassVersionError: Bad version number in .class file".

Ideas?

Posted by Dan Hardiker (217.155.211.5) on May 12, 2007 at 07:46 PM CEST
Website: http://www.adaptavist.com/ #

Thank you! This posting allowed me to figure out how to invoke the Quercus PHP engine from inside ColdFusion 8:

http://corfield.org/entry/ColdFusion_8_running_PHP

Posted by Sean Corfield (67.160.202.22) on June 02, 2007 at 09:02 AM CEST
Website: http://corfield.org #

Dan:

Ludovic mentions that you can grab the JSR-223 jars compiled for Java 5 from the Project Phobos CVS in this blog posting. http://weblogs.java.net/blog/ludo/archive/2007/03/100_java_quercu.html

Hope that helps,

- Matt

Posted by Matt Ingenthron (192.18.101.5) on June 29, 2007 at 03:00 AM CEST
Website: http://blogs.sun.com/mingenthron #

Thank you very much it helped me a lot!!!

Great job ;-)

Posted by Thomas (84.73.174.204) on June 17, 2009 at 02:21 PM CEST #


I recently came accross your blog and have been reading along. I thought I would leave my first comment. I dont know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.so you also can read my website,if you like
ugg boots or want to buy
ugg boots

Posted by ugg boots (59.58.175.182) on December 30, 2009 at 02:29 AM CET
Website: http://www.uggprovide.com #

<strong>Ghd mk4 straighteners</strong> quickly became the essential hair product for anyone with even the slightest frizz in their hair!The range of ghd stylers is ever expanding and improving;The latest limited edition<strong> GHD Styler</strong> to be released is the GHD Precious – a fantastic metallic silver and black styler which is available now in a fabulous GHD Gift Set. This new Pink Set comes in a sizzling new baby pink. The 2009 <strong>mk4 ghd pink</strong> comes with a really lovely looking heat proof pouch. Use the price guide below to find the best online deals for the ghd Pretty in pink hair straighteners.

Posted by ghd mk4 straighteners (113.240.17.123) on January 12, 2010 at 09:22 AM CET
Website: http://www.ghdsmk4.com/ #

hank you very much it helped me a lot!!!
Ludovic mentions that you can grab the JSR-223 jars compiled for Java 5 from the Project Phobos CVS in this blog posting. if you like
Ugg boots or want to buy
Uggs

Posted by ugg boots (59.58.175.182) on January 12, 2010 at 09:55 AM CET
Website: http://www.uggbootsleader.com #

<strong>Mens lacoste shoes </strong> are more and more popular with people now.Laocste Swerve Shoes is welcome by youngsters in modern design, it is the latest in the United States,with the cow leather shoes are made of leather,lacoste canada
also has a good looking.There are string of Lacoste shoes obtainable for equally man and women, where one would exist able to locate his right couple.In addition,the shoes would provide protection of user's foot especially while playing sport.<strong>Lacoste shoes canada</strong> is a new style.It is made with leather.The Mens Lacoste Shoes are durable and fashionable.

Posted by Mens lacoste shoes (113.240.17.123) on January 12, 2010 at 10:22 AM CET
Website: http://www.lacosteshoesstore.com #


Your writing is very elegant, very vivid and lively, I really like you, wish you continued to write better articles, I will often try to concern, oh!

Posted by ugg boots (59.58.175.182) on February 02, 2010 at 09:20 AM CET
Website: http://www.saleuggboots.net #

Post a Comment:

Name:
E-Mail:
URL:

Your Comment:

HTML Syntax: Allowed

Calendar

Links

Search

del.icio.us Tag Cloud

RSS Feeds

Navigation

Referers