passing Python list to Java

1,022 views
Skip to first unread message

Richard van Bemmelen

unread,
Dec 12, 2017, 1:51:33 PM12/12/17
to Py4J Support and Comments
How could I do this? I want to call a function on the Java side with a Python list (containing strings) as argument..
The argument should be placed into a Java String array....

Richard

Richard van Bemmelen

unread,
Dec 12, 2017, 4:31:24 PM12/12/17
to Py4J Support and Comments
Here's what I've tried:
In my Python class constructor I used:
self.li = self.gateway.jvm.java.util.ArrayList()

then I use the following code as response on a button click:
if what == 'btnLoad':      # load templates and send them
res=self.db.select('select name, description from templates')
self.li = []
for r in res:
self.li.append(r[0])
gateway.entry_point.setQRYresul(self.li)

On the Java side, I have this function:

    public void setQRYresult(ArrayList<String> s){
        this.QRYresult = s;
    }  

But in Python I get this error:

Traceback (most recent call last):
  File "/Users/richard/PycharmProjects/CsServer4j/callback_listener.py", line 27, in notify
    gateway.entry_point.setQRYresul(self.li)
  File "/Library/Python/2.7/site-packages/py4j/java_gateway.py", line 1151, in __call__
    args_command, temp_args = self._build_args(*args)
  File "/Library/Python/2.7/site-packages/py4j/java_gateway.py", line 1121, in _build_args
    [get_command_part(arg, self.pool) for arg in new_args])
  File "/Library/Python/2.7/site-packages/py4j/protocol.py", line 290, in get_command_part
    command_part = REFERENCE_TYPE + parameter._get_object_id()
AttributeError: 'list' object has no attribute '_get_object_id'


Op dinsdag 12 december 2017 19:51:33 UTC+1 schreef Richard van Bemmelen:

Barthelemy Dagenais

unread,
Dec 12, 2017, 4:36:40 PM12/12/17
to Richard van Bemmelen, Py4J Support and Comments
Hi Richard, did you try this:

https://www.py4j.org/advanced_topics.html#converting-python-collections-to-java-collections
> --
> You received this message because you are subscribed to the Google Groups
> "Py4J Support and Comments" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to py4j+uns...@py4j.org.
> To post to this group, send email to py...@py4j.org.
> To view this discussion on the web visit
> https://groups.google.com/a/py4j.org/d/msgid/py4j/a3c8f505-434d-416c-be0f-aee085aad84b%40py4j.org.
Reply all
Reply to author
Forward
0 new messages