jpype

要有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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值