The JPype implementation calls an instance of JAVA in python

  • 2020-06-12 09:47:03
  • OfStack

1. JPype briefly

What is JPype?

JPype is a tool that makes it easy for python code to call Java code, overcoming some of the shortcomings of python in areas such as server-side programming.

2. What is the difference between JPype and Jython?

1) Different operating environment: jython runs on jvm, while the actual operating environment of JPype is still python runtime, only one embedded jvm is started during the operation;

2) Different users: jython is for java programs and JPype is for python programmers.

2. JPype installation

1. Install Python2.7 and JAVA1.6 first

2. Install JPype - 0.5.4.2. win32 - py2. 7. exe (http: / / sourceforge net projects/jpype/files/JPype 0.5.4 /)

3. Ubuntu12.04 Installation command: sudo ES54en-ES55en install ES57en-ES58en

3. JPype instructions

1. Start the JVM

The startJVM() function provided by JPype is used to start the JAVA virtual machine, so this method must be called to start the JAVA virtual machine before any subsequent JAVA code is invoked.

The definition of jpype. startJVM ()


startJVM(jvm, *args)

The parameters of jpype. startJVM ()

Parameter 1: jvm, describes the path of the ES86en. dll file on your system, such as "C:\Program Files\ Java50\jre\bin\j9vm.dll". You can get the default JVM path by calling jpype.getDefaultJVMPath ().

Parameter 2: args, which is an optional parameter, will be directly passed by JPype to JVM as the boot parameter of the Java virtual machine. This applies to all valid JVM startup parameters, such as:


 -agentlib:libname[=options] 
 -classpath classpath 
 -verbose 
 -Xint

2. Close JVM

After using JVM, JVM can be turned off with jpype.shutdownJVM (), which has no input arguments. When the python program exits, JVM closes automatically.

3. Reference the third party Java extension

Many times in python projects, the third party Java extension package needs to be called, which is also an important use of JPype.

By adding the JVM boot parameter: -Djava.class.path =ext_classpath, the existing Java extension package is called in python code.

4. Access system properties of JAVA

Sometimes, some Java applications need to set or get system properties in JVM.

Example of setting system variables at JVM startup: Add the following parameters to the startup parameters of JVM:

-Dproperty=value 

4. For example

1. Call JAVA API directly


from jpype import * 
import os.path 
startJVM("C:/Java/jdk1.6.0_10/jre/bin/client/jvm.dll", "-ea") 
java.lang.System.out.println("hello World") 
shutdownJVM() 

2. Call the JAVA third side extension package

JAVA Custom third party jar package: package the JpypeDemo class as jpypedemo.jar file and store it in the F:/sample_Py directory


package jpype; 
 
public class JpypeDemo { 
public String sayHello(String user){ 
return "hello" + user; 
} 
public int calc(int a, int b){ 
return a + b;  
} 
} 

2) Python calls the third party JAVA jar package


from jpype import * 
import os.path 
jarpath = os.path.join(os.path.abspath('.'), 'F:/sample_Py/') 
startJVM("C:/Java/jdk1.6.0_10/jre/bin/client/jvm.dll","-ea", "-Djava.class.path=%s" % (jarpath + 'jpypedemo.jar')) 
#ubuntu  In the startJVM("/home/geek/Android/jdk1.6.0_43/jre/lib/i386/server/libjvm.so","-ea", "-Djava.class.path=%s" % (jarpath + 'XXX.jar')) 
JDClass = JClass("jpype.JpypeDemo") 
jd = JDClass() 
#jd = JPackage("jpype").JpypeDemo() # Two to create jd The method of  
jprint = java.lang.System.out.println 
jprint(jd.sayHello("waw")) 
jprint(jd.calc(2,4)) 
shutdownJVM() 

3. Access system properties of JAVA

Suppose the property you want to set is called yourProperty and the value is yourValue.

1) Example of setting system variables at startup of JVM


import jpype 
 jvmPath = jpype.getDefaultJVMPath() 
 jvmArg =  "  -DyourProperty=yourValue  " 
 if not jpype.isJVMStarted(): 
  jpype.startJVM(jvmPath . jvmArg)

2) Example of setting system variables in the program


import jpype 
 prop =  "  yourProperty  " 
 value =  "  yourValue  " 
 system = jpype.JClass('java.lang.System') 
 system.setProperty(str(prop),str(value))

3) Obtain the example of system variables in the program


import jpype 
 prop =  "  yourProperty  " 
 system = jpype.JClass('java.lang.System') 
 value = system.getProperty(str(prop))

Related articles: