Python橋接Java工具JPype

來源:互聯網
上載者:User

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.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.