Passing and returning Java Arrays from Jython

Friday, September 19, 2008

Accessing java arrays from jython is a little tricky task because one can't assign arrays in a similar way in jython as it would be done in java. Therefore in order to pass an array from jython to java and access an array returned from java in jython, one should use the jarray module.

If you are new to jython, for a customary introduction, jython or JPython is an implementation of Python programming language in JVM, using jython one can directly access java classes and its attributes in addition to the standard python modules. Jython has applications in Unit testing, Server side programming, etc.

In this section we will explore jython arrays using a simple code which will create a java String array from jython, pass the string array to java class which will append an element to the String array and return the updated array to jython which will be printed. So simple isn't it, lets look at the code.


Listing 1:
jythonArray.py


#
# Passing and returning java arrays from jython
# jythonArray.py
# Author: S.Prasanna
#

from java.lang import String
from jarray import array
import javaArray

inputArray = ["one", "two", "three", "four"]
jythonArray = array(inputArray, String)

print "Jython String Array =", jythonArray
print "\nAdding an string %s to jythonArray through javaArray class method...." %("five")
jythonArray = javaArray().append(jythonArray, "five")
print "\nJython String array after adding %s = %s" % ("five", jythonArray)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

Listing 2: javaArray.java


//
// Manipulating jython arrays from java
// javaArray.java
// Author: S.Prasanna
//

public class javaArray {

public javaArray() {
}

//Append an element to to the input String array
public String[] append (String [] inputArray, String element) {
String [] appendArray = new String[inputArray.length + 1];

for (int i = 0; i < class="statement">return
appendArray;
}

public static void main (String args[]) {
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

Output:

Compile the java class and execute the jython script in the same folder, the output will be

C:\> jython jythonArray.py

Jython String Array = array(java.lang.String,['one', 'two', 'three', 'four'])

Adding an string five to jythonArray through javaArray class method....

Jython String array after adding five = array(java.lang.String,['one', 'two', 'three', 'four', 'five'])

Explanation:

The java code (javaArray.java) is straightforward, it has an append method which adds the input String to the input String array and returns the result. Now we will come to the jython code.

Lines 6 - 12
from java.lang import String
from jarray import array
import javaArray

inputArray = ["one", "two", "three", "four"]
jythonArray = array(inputArray, String)
Here we import the array function from jarray module and then call the array function with a list of strings and the type of the array, here we mention the type as "String", similarly we can create array of any java objects by specifying the object class as second parameter. This is equivalent to the java code

String [] jythonArray = ["one", "two", "three", "four"]

Lines 13 - 17

We then pass the jython array to java class's append method where we append a String to the String array, then print the results. The line of importance here is
jythonArray = javaArray().append(jythonArray, "five")
where we instantiate a javaArray object and call its append method, note that creating an instance of a new java object is fairly simple in jython as we did above using javaArray().

1 comment:

Anonymous said...

your code got messed up. what is:

for (int i = 0; i < class="statement">return appendArray;


Copyright © 2008 Prasanna Seshadri, www.prasannatech.net, All Rights Reserved.
No part of the content or this site may be reproduced without prior written permission of the author.