Python桥接Java工具JPype

JPype这玩意可以桥接Java,在Python中调用Java的类,比较好用,对外接口不多,不过需要注意的是python和java类型匹配的问题.可以参考官方文档

http://jpype.sourceforge.net/doc/user-guide/userguide.html

过程中遇到个小问题,设置classpath时,-D前面留了一个空格,导致始终找不到类的错误,很坑爹。

jvmArg = "-Djava.class.path=%s" % ext_classpath
jpype.startJVM(jvmpath,jvmArg)


JPype defines different leels of "match" between Python objects and Java types. These levels are :

  • none, There is no way to convert.
  • explicit (E), JPype canconvert ot the desired type, but only explicitly via the wrapper classes. This means the proper wrapper class will access this type as argument.
  • implicit (I), JPype will convert as needed.
  • exact> (X), Like implicit, but when deciding with method overload to use, one wj=here all the paramters match "exact" will take precedence over "implicit" matches.

 

 
byte
short
int
long
float
double
boolean
char
String
Array
Object
Class
int
I (1)
I (1)
X
I
  
X (10)
     
long
I (1)
I (1)
I (1)
X
        
float    
I (1)
X
      
sequence            
dictionary            
string       
I (2)
X
   
unicode       
I (2)
X
   
JByte
X
           
JShort 
X
          
JInt  
X
         
JLong   
X
        
JFloat    
X
       
JDouble     
X
      
JBoolean      
X
     
JString        
X
 
I (3)
 
JChar       
X
    
JArray         
I/X (4)
I (5)
 
JObject         
I/X (6)
I/X (7)
 
JavaObject          
I (8)
 
JavaClass          
I (9)
X

(1) Conversion will occur if the Python value fits in the java native type.

(2) Conversion occurs if the python string or unicode is of length 1

(3) The required object must be of a type compatible with java.lang.String (java.lang.Object, java.util.Comparable)

(4) Number of dimensions must match, and the types must be compatible

(5) Only when the required type is java.lang.Object

(6) Only if the JObject wrapper's specified type is an compatible array class.

(7) Only if the required type is compatible with the wrappers's specified type. The actual type of the java object is not considered.

(8) Only if the requireds type is compatible with the Java Object actual type.

(9) Only when the required type is java.lang.Object or java.lang.Class

(10) only the values True and False are implitly converted to booleans.

Converting from java to python

The rules here are much simpler.

Java byte, short and int are converted to python int.

Java long are conversion to Python long

Java float and double are converted to python float.

Java boolean are converted to python int of value 1 or 0

Java char are converted to python unicode of length 1.

Java String are converteds to python unicode.

Java arrays are converteds to JArray

All other java object are converted to JavaObject.

Java Class are converted to JavaClass.

Java array Class are converted to JavaArrayClass.



使用 `jpype` 调用 Java 包需要先安装 `jpype` 库,可以使用 `pip` 命令进行安装: ``` pip install jpype1 ``` 然后,可以按照以下步骤使用 `jpype` 调用 Java 包并传参: 1. 首先,需要启动 JVM 进程,可以使用 `jpype.startJVM()` 函数来启动: ```python import jpype jpype.startJVM() ``` 2. 导入 Java 类: ```python java_class = jpype.JClass('com.example.MyClass') ``` 其中,`com.example.MyClass` 是要调用的 Java 类的全限定名。 3. 创建 Java 对象: ```python java_object = java_class() ``` 4. 调用 Java 方法: ```python result = java_object.myMethod('param1', 'param2') ``` 其中,`myMethod` 是要调用的 Java 方法名,`'param1'`、`'param2'` 是传递给 Java 方法的参数。 5. 关闭 JVM: ```python jpype.shutdownJVM() ``` 完整示例代码如下: ```python import jpype # 启动 JVM jpype.startJVM() # 导入 Javajava_class = jpype.JClass('com.example.MyClass') # 创建 Java 对象 java_object = java_class() # 调用 Java 方法 result = java_object.myMethod('param1', 'param2') # 关闭 JVM jpype.shutdownJVM() ``` 注意,如果 Java 类需要传递复杂类型的参数,需要先将 Python 对象转换为 Java 对象。可以使用 `jpype.java` 模块中的函数进行转换,例如: ```python import jpype # 启动 JVM jpype.startJVM() # 导入 Javajava_class = jpype.JClass('com.example.MyClass') # 创建 Java 对象 java_object = java_class() # 转换 Python 对象为 Java 对象 param1 = jpype.java.lang.String('hello') param2 = jpype.JArray(jpype.JInt)([1, 2, 3]) # 调用 Java 方法 result = java_object.myMethod(param1, param2) # 关闭 JVM jpype.shutdownJVM() ``` 在上面的示例中,`jpype.java.lang.String` 将 Python 字符串转换为 Java 字符串,`jpype.JArray` 将 Python 列表转换为 Java 数组。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值