Python bridge Java tool jpype

Source: Internet
Author: User

Jpype can be used to bridge Java and call Java classes in Python. It is easy to use and has few external interfaces. However, you need to note the matching of Python and Java types. For details, refer to the official documentation.

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

A small problem occurs during the process. When classpath is set, a space is left in front of-D, leading to the failure to find the class error.

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 compatibleJava. 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 isJava. 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 isJava. Lang. ObjectOr
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.

JavaByte, short and intAre converted to PythonInt.

JavaLongAre conversion to PythonLong

JavaFloat and doubleAre converted to PythonFloat.

JavaBooleanAre converted to PythonIntOf value 1 or 0

JavaCharAre converted to PythonUnicodeOf length 1.

JavaStringAre converteds to PythonUnicode.

JavaArraysAre convertedsJarray

All other Java object are convertedJavaobject.

JavaClassAre convertedJavaclass.

Java ArrayClassAre convertedJavaarrayclass.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.