Hi Johann: We embed JSR 223 and Jython support in PushToTest
TestMaker (http://www.pushtotest.com). We did not encounter the
problem you mentioned when calling Jython 2.1 through the JSR 223
I thought you should know about a problem you are bound to encounter.
When we define a class in Jython, we were not able to obtain an
instance of it from Java. That is, someone in the Jython script must
instantiate the class and return the object reference. We work around
this problem by requiring the Jython scripts to include a getXXXX()
method that instantiates and returns the object reference.
Why so? Because the interpreter simply interprets the script, and
there is nothing directly executable, but just declarations. In this
particular case, "print 1" is an executable line, so it will execute.
The line "1 + 1" also executes, but the eval() method returns an
object. It may be that Jython is evaluating the addition but is not
returning the value (since it is not being assigned), thus the engine
returns null. Try "a = 1 + 1" instead. In this case, the addition is
actually going into a variable, that may be returned. You may also
note that it may return an integer, that must be translated into a
Java object. That I don't know if Jython library supports it.
Hope this helps.
-Frank (and William)
On Aug 6, 2007, at 1:36 AM, Johann Petrak wrote:
> I try to embed jython using the jsr223 scripting engine interface
> (using java 6 on Linux for this).
> This is part of an attempt to add several scripting engines to my
> application in a consistent way, and I have so far gotten the jruby,
> However, with the jython engine, there is the following problem:
> Unlike with the other engines, evaluating an expression always
> returns null.
> Here is a rough outline of what I do (removed error checking etc.):
> ScriptEngineManager scriptEngineManager =
> new ScriptEngineManager(myOwnClassLoader);
> // this returns non-null:
> scriptEngine = scriptEngineManager.getEngineByName("jython");
> // ....
> Object value = scriptEngine.eval(command);
> If command is e.g. "print 1" then 1 appears on standard output, so
> the scripting engine works somehow.
> However, variable value always contains null.
> When command is e.g. 1 + 1 or "asdf"+"asdf", value should contain
> the evaluated expression but still contains null.
> I am using exactly the same code as for other scripting engines, which
> all return the correct value. However, the jython engine always
> return null.
> Any ideas what could be wrong here?
> This SF.net email is sponsored by: Splunk Inc.
> Still grepping through log files to find problems? Stop.
> Now Search log events and configuration files using AJAX and a
> Download your FREE copy of Splunk now >> http://get.splunk.com/
> Jython-users mailing list
Frank Cohen, PushToTest, http://www.PushToTest.com, phone 408 374 7426
TestMaker: The open-source SOA test automation tool