我面临来自单例对象的getInstance的一些问题,因为jpype需要一个默认构造函数来实例化类,但是单例不具有默认构造函数或者只是保持私有以通过getInstance方法强制获取实例。当我尝试获取实例时,我在下面看到了这个错误:
Traceback (most recent call last):
File "/home/myuser/PycharmProjects/PythonTests/JavaTests.py", line 21, in <module>
service = jpype.JClass("com.classpackage.Service")
File "/usr/lib64/python2.7/site-packages/jpype/_jclass.py", line 54, in JClass
raise _RUNTIMEEXCEPTION.PYEXC("Class %s not found" % name)
jpype._jexception.ExceptionPyRaisable: java.lang.Exception: Class com.classpackage.Service not found
下面的代码
import jpype
jpype.startJVM("/usr/java/jdk1.7.0_79/jre/lib/amd64/server/libjvm.so",
"-Djava.class.path=Service.jar")
jpype.java.lang.System.out.println("Hello!!!")
MODEL_PACKAGE = "com.classpackage"
SERVICE_PACKAGE = "com.classpackage"
info = jpype.JPackage(MODEL_PACKAGE).Info()
jpype.java.lang.System.out.println("Hello 2!!!")
#the error happens in this line below. This class service is a singleton.
service = jpype.JClass("com.classpackage.Service")
jpype.java.lang.System.out.println("Hello 3!!!")
jpype.shutdownJVM()
任何人都知道在python中使用jpype在java中使用单例的解决方法吗?
感谢。
答案 0 :(得分:0)
使用
Service = jpype.JPackage("com.classpackage").Service
代替。然后拨打
instance = Service.getInstance()
获得singelton。