59185

Get data back from Jython scripts using JSR-223

I am using Jython 2.5.1 with JSR-223 (i.e. javax.script package) and I expect the last line of the Python script to be returned. For example, after evaluating this script:

class Multiplier: def multiply(self, x, y): return x * y Multiplier().multiply(5, 7)

I should get back 35, but I get null instead. In other hand it works with this other test:

5 * 7

What am I doing wrong?

Here's the Java code:

public static void main(String[] args) throws Exception { ScriptEngine engine = new ScriptEngineManager().getEngineByName("python"); FileReader f = new FileReader("Multiplier.py"); Object result = engine.eval(f); //assert(result == 35); }

PS: It works fine with JRuby, Groovy and Rhino, i.e. the last line is always returned.

Thanks in advance.

Answer1:

<strong>UPDATE:</strong> I was actually missing the goal (and problem) of the OP in my initial answer that has been clarified in a comment. I'm updating my answer accordingly.

First update the Multiplier.py script as below:

class Multiplier: def multiply(self, x, y): return x * y x = Multiplier().multiply(5, 7)

Then call it like this from the Java code:

public static void main(String[] args) throws Exception { ScriptEngine engine = new ScriptEngineManager().getEngineByName("python"); FileReader f = new FileReader("Multiplier.py"); engine.eval(f); Object x = engine.get("x"); System.out.println("x: " + x); }

I get the following output when running the code above:

x: 35

Answer2:

This is a Python language issue more than a Jython or JSR 223 issue. Python differentiates between expressions (which have values) and statements (which don't). The script you're passing is a statement. If you passed an expression, it'd have a value.

The reason you're seeing something different with Ruby and JavaScript is that compound statements have the value of the last statement evaluated. For example, compare Ruby:

>> (2 ; 3) + 5 => 8 >> (x = 5) + 7 => 12

with Python:

>>> (2 ; 3) + 5 File "<stdin>", line 1 (2 ; 3) + 5 ^ SyntaxError: invalid syntax >>> (x = 5) + 7 File "<stdin>", line 1 (x = 5) + 7 ^ SyntaxError: invalid syntax

JavaScript seems to be somewhere in between. Like Ruby, assignments evaluate to the value assigned. However, the last evaluated statement in a block is returned but not usable as part of an expression:

> { 2 ; 3 } 3 > { 2 ; 3 } + 5 5 > (x = 5) + 7 12

Answer3:

I had the same problem. I have written a scripting library that supports JSR223 and have submited http://bugs.jython.org/issue1798:

$ python Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56) [GCC 4.4.5] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> x=5<br/> >>> x<br/> 5 >>>

At least if you eval("x=5\nx") it seems like you would get the value back.

Recommend

  • Execute Java code that is stored in the database
  • Nashorn under Java 7
  • Does it make sense to load scripts concurrently in Java 8 Nashorn JavaScript engine
  • Java execute Embedded Python with parameters
  • getNodeValue() returns null although response xml holder does not
  • Are Google Chrome Base64 methods capable of handling binary data from the File API?
  • How could I write a BsonDocument object into a file, and read it again, using Java
  • Kentico Repeater HTML Properties showing with selected transformation
  • How to change display text in item template based on eval in gridview?
  • Development workflow for server and client using Docker Compose?
  • Pytest/Allure - How to generate testcase description?
  • Accessing Windows Azure Queues from client side javascript/jquery
  • Paramiko SSHException Channel Closed
  • Doctrine2 inverse persistance not working in nested forms
  • How to enable large page memory for the JVM?
  • Python 3.2.2, error(scripts to exe)
  • vectorized indexing/slicing in numpy/scipy?
  • pillow imaging ImportError
  • Intel-64 and ia32 atomic operations acquire-release semantics and GCC 5+
  • Ajax Upload File: $_FILES is empty but files exists in request header
  • Access variable of ScriptContext using Nashorn JavaScript Engine (Java 8)
  • Django simple Captcha “No module named fields” error
  • How to know when stdin is empty if it contains EOF?
  • Sony Xperia Z Tablet not found by adb
  • How to recover from a Spring Social ExpiredAuthorizationException
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • javascript inside java/jsp code
  • Websockets service method fails during R startup
  • How can I estimate amount of memory left with calling System.gc()?
  • AT Commands to Send SMS not working in Windows 8.1
  • php design question - will a Helper help here?
  • Android Studio and gradle
  • AngularJs get employee from factory
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Hits per day in Google Big Query
  • How do I configure my settings file to work with unit tests?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • Is it possible to post an object from jquery to bottle.py?
  • How can i traverse a binary tree from right to left in java?