AttributeError when accessing a member of a 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 in .jar. Then I include this .jar file using -Dpython.path = "path / to / jar / my.jar". So far so good, no complaints when starting my program.

However, when I get to some of the code using my java class, it seems like it cannot find any 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 examples 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() 
+4
source share
2 answers

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

 python.security.respectJavaAccessibility = false 

added to the Jython registry. See http://www.jython.org/jythonbook/en/1.0/appendixA.html?highlight=accessibility#registry-properties .

Or make the class public.

+2
source

You must import the Java class into Jython code before you can use it. Sort of:

 from pos import Test test = Test() print 'Name = ', test.getName() 

See Chapter 8 of the Jython book for an understanding of import paths, etc. work in jython.

0
source

All Articles