Accessing static java methods in Python via jython

I'm currently trying to access a static class in java, inside python. I import as usual, then I try to get an instance of the java class class.

from com.exmaple.util import Foo Foo. __class___.run_static_method() 

This does not work. offers? What am I doing wrong.

+4
source share
5 answers

Try using

 Foo.run_static_method() 
+1
source

I assume that you instantiate the class and just call the method.

 from com.example.util import Foo foo = Foo() foo.run_static_method() 

Assuming just executing Foo.run_static_method() not working.

+1
source

It works as follows:

 Jython 2.5.1 (Release_2_5_1:6813, Sep 26 2009, 13:47:54) [Java HotSpot(TM) Client VM (Sun Microsystems Inc.)] on java1.6.0_22 Type "help", "copyright", "credits" or "license" for more information. >>> import java.lang >>> java.lang.System.getProperty('user.dir') u'/home/vinay' 

Note that getProperty is a static method of the static class java.lang.System .

+1
source

I came across this class, which only stores static methods:

 public class foo { public static void bar() { ... } } 

Adding a dummy constructor helped in my case. I assume that due to the nature of pythons, classes were actually already objects (there is a long post on metaclasses giving some information about understanding a class in python, reading it worth reading eventhough is another topic), and jython is trying to make the class an object before running the function although it is static. I am sure this may be an error message. (I am testing on jython2.5).

update . I don’t think my theroy for theause is likely, since I think Java has some pure static classes as well. However, the solution solved the problem twice.

with dummy constructor:

 public class foo { public foo() {} //!This dummy constructor did the trick for me public static void bar() { ... } 

}

+1
source

I ran into this problem. There is a question that other defendants are not aware of. If the Java class does not have the public keyword, then its static methods are not available to Jython. This is confusing because it is independent of whether the methods themselves are public and other ways of accessing an implicitly public class, such as an instance. So do the following:

 public class foo { public static void bar() { ... } } 

Not this:

 class foo { public static void bar() { ... } } 
0
source

All Articles