5

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()
mzjn
  • 48,958
  • 13
  • 128
  • 248
user2144763
  • 53
  • 1
  • 4

2 Answers2

3

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.

mzjn
  • 48,958
  • 13
  • 128
  • 248
0

You must import the Java class in the Jython code before you can use it. Something like:

from pos import Test

test = Test()
print 'Name = ', test.getName()

See Chapter 8 of the Jython Book to understand how import paths etc. work in Jython.

Joonas Pulakka
  • 36,252
  • 29
  • 106
  • 169
  • Sorry, I should have included more code. I have actually imported my class in the jython file I just didn't include it in the code snippet. So this isn't the problem. – user2144763 Apr 09 '13 at 18:46