Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError when accessing member of Java class in Jython

I am trying to import my own java class into some jython code. I compiled my .java to a .class file and put the .class file into a .jar. I then include this .jar file using -Dpython.path="path/to/jar/my.jar". So far so good, no complaints when starting up my program.

However, when I get to the part of the code that uses my java class, it seems like it can't find any of the functions in my java class. I get the following AttributeError:

AttributeError: 'pos.Test' object has no attribute 'getName'

Any suggestions would be much appreciated! (Code samples below.)

Java code:

package pos;

class Test{

    private String name;

    public Test(){
        name = "TEST";
        System.out.println( "Name = " + name );
    }

    public String getName(){
        return name;
    }   
}

Jython code snippet:

import pos.Test

...

test = pos.Test()

print 'Name = ', test.getName()
like image 205
user2144763 Avatar asked Oct 22 '22 12:10

user2144763


1 Answers

It is about visibility. Your class is package-private. It will work if

python.security.respectJavaAccessibility = false

is added to the Jython registry. See https://www.jython.org/registry.html.

Or make the class public.

like image 186
mzjn Avatar answered Oct 29 '22 21:10

mzjn