Passing a java object in python

I am prototyping the interface for our application to allow other people to use python, our application is written in java. I would like to pass some of our data from a java application to python code, but I'm not sure how to pass an object to python. I made a simple call to the java-> python function using simple parameters with Jython and found it to be very useful for what I'm trying to do. Given the following class, how can I use it in Python / Jython as an input to a function / class:

public class TestObject { private double[] values; private int length; private int anotherVariable; //getters, setters } 
+6
source share
3 answers

One solution. You can use some kind of message system, queue or broker of some kind to serialize / deserialize or transfer messages between python and java. Then create sorters / producers / consumers to work on the queues that will be processed in python or java.

Also consider checking out inspiration: https://www.py4j.org/

py4j is mainly used for / for pyspark and hadoop type.

To answer your question more immediately.

An example of using json-simple .:

 import org.apache.commons.io.FileUtils; import org.json.simple.JSONObject; //import org.json.simple.JSONObject; public class TestObject { private double[] values; private int length; private int anotherVariable; private boolean someBool; private String someString; //getters, setters public String toJSON() { JSONObject obj=new JSONObject(); obj.put("values",new Double(this.values)); obj.put("length",new Integer(this.length)); obj.put("bool_val",new Boolean(this.SomeBool)); obj.put("string_key",this.someString); StringWriter out = new StringWriter(); obj.writeJSONString(out); return out.toString(); } public void writeObject(){ Writer writer = new BufferedWriter( new OutputStreamWriter( new FileOutputStream("anObject.json"), "utf-8") ) ) writer.write(this.toJSON()); } public static void setObject(){ values = 100.134; length = 12; anotherVariable = 15; someString = "spam"; } } 

And in python:

 class DoStuffWithObject(object): def __init__(self,obj): self.obj = obj self.changeObj() self.writeObj() def changeObj(self): self.obj['values'] = 100.134; self.obj['length'] = 12; self.obj['anotherVariable'] = 15; self.obj['someString'] = "spam"; def writeObj(self): ''' write back to file ''' with open('anObject.json', 'w') as f: json.dump(self.obj, f) def someOtherMethod(self, s): ''' do something else ''' print('hello {}'.format(s)) import json with open('anObject.json','r') as f: obj = json.loads(f.read()) # print out obj['values'] obj['someBool'] ... for key in obj: print(key, obj[key]) aThing = DoStuffWithObject(obj) aThing.someOtherMethod('there') 

And then in java read the object. There are solutions that implement this idea (JSON-RPC, XML-RPC and options). Depending on this, you might also want to consider something like ( http://docs.mongodb.org/ecosystem/drivers/java/ ) that mongo does json.

Cm:

A more complete list of queues:

Related Resources:

+4
source

Agree with the answer below. I think the bottom line is that "Python and Java are separate shell interpreters." Therefore, you should not expect the transfer of an “object” from one to another. You should not expect a "method call". But it is reasonable to transfer data from one to another by serializing and de-serializing it through some intermediate data format (for example, JSON), as with any other program.

In some environments, such as Microsoft Windows, it is possible that technology such as OLE (point-to-network) can be used to allow environments to be “actively connected”, where various systems implement and provide OLE objects. But I have no personal experience with how and how to do it.

Therefore, the safest thing is to consider them as “records” and use serialization methods on both sides. (Or, if you are very adventurous, run (say) Java in a child thread.) An “adventurous” design can quickly get out of hand with little return on investment.

0
source

You need to make a python exe file using py2exe, link: https://www.youtube.com/watch?v=kyoGfnLm4LA . Then use the program in java and skip the arguments:

Please refer to this link for details:

Fortran90 exe ​​program call from java fails

0
source

Source: https://habr.com/ru/post/987026/


All Articles