简体   繁体   中英

Python access to objects created by Java-Application during runtime

I have a Java-Application given as a .jar-file and a third-party progam, which has a Python-API. The workflow is that I start the java-Application insert there some data and also some new objects get created. After that I want to press a button maybe, which executes the Python script to transfer this data to the third-party programm. The Python script needs data stored in the created objects.

So my questions are:
(1) is it possible to give the Python script an array as an input argument?

or (2) is there a way to give the Python script a Java object as an input and the script could access the methods, child objects and variables of the given Object? eg: that I could write something like this in the Python script:

data = JavaObject //the Object the Python script received
i = data.object1.getFoo()
print(i)

or (3) is there a way to embed the Python script into the .jar-file that i can execute it and the Python script has access to the classes and methods of the .jar-file? eg:

i = Object.object1.getChild().getValue() //Object should be any object
                                         //which i created previously

I think that Jython would have been good for this kind of task, but it only supports Python 2.7 and due to the third-party API i need to work with Python 3.5 If necessary it would be possible to use Python 3.3 or 3.4, but only if needed.

EDIT: If possible the Java-Application should be executeable without Python installed, but that is not necessary would just be a nice feature

After more searching and discussing with colleagues, I think I can answer my own question.

You can use Py4J to get variables and objects from java.
After reading the tutorial everything else was easy. It does what i want and i can export the data quicker now.

It is also possible to write some data back into the Java-Application. If you have implemented setter-methods you can call them from Python.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM