from jpype import startJVM, shutdownJVM, java, addClassPath, JClass, JInt startJVM(convertStrings=False) import jpype.imports #addClassPath("org/pkg") #from org.pkg import MyMath #import MyMath #import org.pkg as p #print(p) #print(p.__class__) try: pass #print(dir(p)) #p.MyMath.divide(6, 2) #import Calculator calc = JClass('Calculator') print(calc) print(dir(calc)) res = calc.add(java.lang.Integer(2), java.lang.Integer(2)) print(res) #from org.pkg import Calculator math = JClass('org.pkg.MyMath') res = math.divide(java.lang.Integer(6), java.lang.Integer(2)) #res = math.divide(JInt(6), java.lang.Integer(2)) print(res) #calc = Calculator() #print(calc) except Exception as err: print(f"Exception: {err}")
def add(self, word): hanlp = JClass('com.hankcs.hanlp.dictionary.CustomDictionary') hanlp.add(word)
import jpype from jpype import JImplements, java, JClass, JObject, JOverride jpype.startJVM(convertStrings=False) @JImplements(java.io.Serializable) class MyClass(object): def __init__(self, mesg): self.mesg = mesg def getMessage(self): return self.mesg @JOverride def toString(self): return self.mesg jl = JClass('java.util.ArrayList')() jl.add(MyClass("foo")) print(jl.get(0)) print(type(jl.get(0))) # does not work with jpype 0.7 but is implemented due to print(jl.get(0).getMessage())