Bug in calling a default constructor

172 views
Skip to first unread message

y...@bezman.net

unread,
Jan 2, 2018, 4:54:10 AM1/2/18
to Py4J Support and Comments
When defining a class without an explicitly defined default constructor, constructing that class is possible from Java, but not from py4j.

The exception thrown is:

Traceback (most recent call last):

  File "a.py", line 51, in <module>

    a = gateway.jvm.A()

  File "~/user/ext/noarch/pylib/site-packages/py4j/java_gateway.py", line 1428, in __call__

    answer, self._gateway_client, None, self._fqn)

  File "~/user/ext/noarch/pylib/site-packages/py4j/protocol.py", line 324, in get_return_value

    format(target_id, ".", name, value))

py4j.protocol.Py4JError: An error occurred while calling None.A. Trace:

py4j.Py4JException: Constructor A([]) does not exist

        at py4j.reflection.ReflectionEngine.getConstructor(ReflectionEngine.java:179)

        at py4j.reflection.ReflectionEngine.getConstructor(ReflectionEngine.java:196)

        at py4j.Gateway.invoke(Gateway.java:237)

        at py4j.commands.ConstructorCommand.invokeConstructor(ConstructorCommand.java:80)

        at py4j.commands.ConstructorCommand.execute(ConstructorCommand.java:69)

        at py4j.GatewayConnection.run(GatewayConnection.java:214)

        at java.lang.Thread.run(Thread.java:745)


Thanks in advance for any help.

Barthelemy Dagenais

unread,
Jan 2, 2018, 9:28:21 AM1/2/18
to Yevgeny Bezman, Py4J Support and Comments
Hi,

Your class is not public, it is package-private. The default
constructor is thus also package-private
(https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.8.9)
so Py4J does not see it. You can add the public keyword to your class
or create a public method that instantiates your package-private class
and call the public method from Py4J.

On Tue, Jan 2, 2018 at 9:20 AM, Yevgeny Bezman <y...@bezman.net> wrote:
> I've defined my class as follows:
> class A
> {
> }
>
> calling new A() in Java works, but gateway.jvm.A() doesn't.
>
>
>
> On Tue, Jan 2, 2018 at 4:07 PM, Barthelemy Dagenais
> <barth...@infobart.com> wrote:
>>
>> Hi,
>>
>> I don't think the problem is with Py4J. What is the source code of
>> class A? Are you declaring another constructor (in that case, there is
>> no default constructor).
>>
>> Example:
>>
>> public class TestNoConstructor {
>> public String m1() {
>> return "Hello";
>> }
>> }
>>
>> Python code:
>>
>> In [1]: from py4j.java_gateway import JavaGateway
>>
>> In [2]: gateway = JavaGateway()
>>
>> In [3]: t = gateway.jvm.TestNoConstructor()
>>
>> In [4]: t.m1()
>> Out[4]: 'Hello'
>> > --
>> > 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/e5dc1fa4-24e6-422a-9404-688162fe512b%40py4j.org.
>
>
Reply all
Reply to author
Forward
0 new messages