当前位置: 首页 > 工具软件 > JPype > 使用案例 >

jpype

张宣
2023-12-01

要有jvmPath

可以 jpype.getDefaultJVMPath()获得

 然后jpype.startJVM(jvmPath)打开jvm

 javaClass = jpype.JClass('com.xx.xx') 

javaInstance = javaClass(value) //构造函数

javaInstance.xxxx( “ xxxxx ” )//方法调用

调用完毕后jpype.shutdownJVM()

底层实现跟jni有关系。

import jpype 
from jpype import JavaException 

jvmPath = jpype.getDefaultJVMPath()           #the path of jvm.dll 
classpath = "F:\\test\\cipher"                 #the path of PasswordCipher.class 
jvmArg = "-Djava.class.path=" + classpath 
if not jpype.isJVMStarted():                    #test whether the JVM is started 
    jpype.startJVM(jvmPath,jvmArg)             #start JVM 
javaClass = jpype.JClass("PasswordCipher")   #create the Java class 
try: 
    testString = "congratulations" 
    print "original string is : ", testString 
    #call the run function of Java class 
    encryptedString = javaClass.run("encrypt", testString) 
    print "encrypted string is : ", encryptedString 

    #call the run function of Java class 

    decryptedString = javaClass.run("decrypt", encryptedString) 
    print "decryped string is : ", decryptedString 
except JavaException, ex: 
    print "Caught exception : ", JavaException.message() 
    print JavaException.stackTrace() 
except: 
    print "Unknown Error" 
finally: 
    jpype.shutdownJVM()        #shut down JVM 

 

------------------

 

packageinfo.leyond.test;
 
publicclassTest {
    privateString msg;
 
    publicTest() {
        msg ="nothing so far...";
    }
 
    publicstaticvoidspeak(String msg) {
        System.out.println(msg);
    }
 
    publicvoidsetString(String s) {
        msg = s;
    }
 
    publicString getString() {
        returnmsg;
    }
}

----------------------------pppppppppppppppppppppppython--------------------

fromjpypeimport*
 
startJVM("C:/Program Files/Java/jdk1.6.0_21/jre/bin/client/jvm.dll",
"-ea -Djava.class.path=C:\\Documents and Settings\\Administrator\\eclipseworkspace\\Trajectory\\src\\report")
 
testPkg=JPackage('info').leyond.test
Test=testPkg.Test
 
Test.speak("Welcome to www.leyond.info")
t=Test()
printt.getString()
 
shutdownJVM()
 
 

 另外At the moment, it is not possible to use threads created from Java, since there is no callback available

Non-static inner classes cannot be instantiated from Python code. Instances received from Java code thay can be used without problem 

 Write it all in Python, then write only those parts that need it in C(java)

再copy就copy个没完了,直接传送门:http://jpype.sourceforge.net/doc/user-guide/userguide.html

转载于:https://my.oschina.net/lyxung/blog/179462

 类似资料:

相关阅读

相关文章

相关问答