py4j / py4j

Py4J enables Python programs to dynamically access arbitrary Java objects

Home Page:https://www.py4j.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ArrayList.append() doesn't seem to work with Java 16/17

tueda opened this issue · comments

Consider the following test code (complete version is here):

l = gateway.jvm.java.util.ArrayList()
l.append(1)
l.add("hello")
print(l)
for elem in l:
    print(elem)

This code prints, with Java 15 or earlier:

[1, 'hello']
1
hello

as expected. But with Java 16/17 it prints an empty list, unfortunately:

[]

This can be reproduced on Docker:

docker run -it --rm openjdk:17-jdk-slim-buster /bin/bash

and then in the container:

apt update
apt install -y curl python3-pip
pip3 install py4j==0.10.9.2
curl -s https://gist.githubusercontent.com/tueda/501c01d5112d7e8e5f127ca59faf2c78/raw/5c18c4acbaba42eae2a92390926649e659394bff/py4j_test1.py | python3 -

Edit:
The above commands in the openjdk:17-jdk-slim-buster image use openjdk 17.0.1 / Python 3.7.3 / pip 18.1.
Related: #403

Could you please check if #453 fixes this for you? Thanks!

@torokati44 Nice! I tried your support-java-17 branch (c39335d) and it printed the correct result with the above example.

For reference, what I did as follows. First, I built a wheel with an older JDK (or I need to change sourceCompatibility and targetCompatibility to at least 1.7 for JDK17).

docker run -it --rm -v $(pwd):/work openjdk:11-jdk-slim-buster /bin/bash
apt update
apt install -y git python3-pip
git clone https://github.com/torokati44/py4j.git -b support-java-17
cd py4j
python3 setup.py bdist_wheel
cp dist/py4j-0.10.9.3-py3-none-any.whl /work

Then, I tested the created wheel with Java 17.

docker run -it --rm -v $(pwd):/work openjdk:17-jdk-slim-buster /bin/bash
apt update
apt install -y curl python3-pip
pip3 install /work/py4j-0.10.9.3-py3-none-any.whl
curl -s https://gist.githubusercontent.com/tueda/501c01d5112d7e8e5f127ca59faf2c78/raw/5c18c4acbaba42eae2a92390926649e659394bff/py4j_test1.py | python3 -

I have the same issue. I was trying out the quickstart examples.

java_list = gateway.jvm.java.util.ArrayList()
java_list.append(214)
java_list.append(120)
print(len(java_list))
print(java_list[0],java_list[1])
print(java_list)
for i in java_list:
    print('num: ', i)

With the above code I get the output below:

2
214 120
[]

I seems, I can index properly as well as get the length. The code below does work:

for i in range(len(java_list)):
    print('num: ', java_list[i])
num:  214
num:  120

So arraylist seems to be working, but not completely.

My system:
openjdk 17.0.1 2021-10-19
OpenJDK Runtime Environment (build 17.0.1+12-Ubuntu-120.04)
OpenJDK 64-Bit Server VM (build 17.0.1+12-Ubuntu-120.04, mixed mode, sharing)
Python 3.8.12

Hey guys, we plan to work on it. I will leave it closed this as a duplicate of #485. Let's track it there.