Passing 2D arrays to Java from Python using javabridge

There are many libraries for calling java code from python and there are many articles that try to provide a preview on their syntax. I’ve worked with JPype and Javabridge which seems to be two of the more famous ones. While JPype is by far more use friendly that javabridge I have personally had problems with it when trying to add some jar files to the class path. Especially if the jar file is packaged by maven or the project’s structure is a bit more complicated or unconventional.

However, javabridge has its own problem which is mostly lack of documentation. The general use case is pretty clear from the documentation here. But once you want to get into a bit more detail use cases or even a bit more serious function calls like passing a 2D array you will find yourself lost. While they provide a good explanation of type definitions when trying to call a function with specific arguments here figuring out how is to create objects as common as a 2D array like double[][] or even double[] is not to easy to find. In fact it seems like closest thing you can find to a documentation is this unit test module. In this article I will show how to create a 2D int array (int[][]) in python and how to pass it to a Java function!

We will first assume we have a simple java class:


Assuming we have create a jar file that includes our class with the name testjb.jar we can use out class and run method as bellow:

What is happening in the code above is that we are creating a Python class TestObjectAPI that has a pointer to the Java class in new_fn. In the __init__ method we are calling new_fn and hence creating an instance of our TestObject. Also javabridge.make_method function is creating a new python method for the class that calls our run method from TestObject in java. The second parameter “([[I[[ID)Ljava.lang.String;” indicates the argument and return type definition and is essential for finding the class method. It is generally in form of “(..)..” and anything inside the parentheses shows the arguments of the method and after the parenthesis determines the return type. For example “(ID)V” means two arguments int and double and void as return type. More information can be found here . What we have in the example means that we have two 2D arrays  of type int”[[I” and one double “D” and the return type is Java String. Note the “;” at the end of String’s classpath. It is necessary in determining the end of the classpath. Most of what I have said so far couldn’t some how be found from javabridge’s website. However, now we need to create a 2D array. You can create a 2D array using the following code:

Something that you might not be used to in Python is making sure you are using the right type of value for your numpy array when passing to java. For example if you are creating a int[][] you should make the numpy values are of type “np.int32”. So you can either do this using “astype” method on a numpy array/matrix or when converting a list to numpy array you can dp “np.array(X,dtype=np.int32)”

I hope this saves you some time from unnecessary searching and reading source codes and please let me know what you think


Leave a reply:

Your email address will not be published.

Site Footer