jpype module, python calls java's interface

forward from:

http://www.cnblogs.com/junrong624/p/5278457.html

https://www.cnblogs.com/fanghao/p/7745356.html

 

1. Brief introduction of JPype

1. What is JPype?

     JPype is a tool that allows python code to easily call Java code, thus overcoming the shortcomings of python in some areas (such as server-side programming).

2. What is the difference between JPype and Jython (the successor of JPython)?

  1) The running environment is different: jython runs on jvm, while the actual running environment of JPype is still python runtime, but an embedded jvm is started during running;

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

3. Installation

  yum install python-jpype

 

2. Instructions for using JPype

import jpype

#Get the jvm path of the system 
jvmPath = jpype.getDefaultJVMPath() #Start
 the virtual machine 
jpype.startJVM(jvm= jvmPath) #Execute 
 the JAVA code 
jpype.java.lang.System.out.println( " hello world " )
 #Close the virtual machine 
jpype.shutdownJVM()

 1. Start the JVM

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

  • Definition of jpype.startJVM()
startJVM (jvm, * args)
  • Parameters to jpype.startJVM()

    Parameter 1: jvm, describe the path of the jvm.dll file in your system, such as " C:\Program Files\IBM\Java50\jre\bin\j9vm\jvm.dll ". The default JVM path can be obtained by calling jpype.getDefaultJVMPath().

    Parameter 2: args, an optional parameter, will be directly passed to the JVM by JPype as the startup parameter of the Java virtual machine. All valid JVM startup parameters fit here, for example:

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

 2. Shut down the JVM

        When you are done using the JVM, you can shut down the JVM with jpype.shutdownJVM(), which takes no input parameters. When the python program exits, the JVM shuts down automatically.

 

=============================================================================================================

from jpype import *  
import os.path

#Open the JVM and specify the location of the jar package jarpath 
= os.path.join(os.path.abspath( ' . ' ), ' /data/html/licenseVerify.jar ' ) 
dependency_lib_path = os.path.join(os.path .abspath('.'), '/data/html/') jpype.startJVM(jpype.getDefaultJVMPath(),
"-Djava.class.path=%s" % jarpath, "-Djava.ext.dirs=%s" % dependency_lib_path)
#Introduce the class in the java program. The path should be the package package path in the project 
javaClass = JClass( " jpype.JpypeDemo " )  
test = javaClass()

#Pass in the actual parameter 
test.setParam( ' /data/html/param.properties ' ) to the class test

#Get the return value from the java class 
value = test.getProperty(str(prop))

shutdownJVM()  

 3. Refer to the third-party Java extension package

       In many cases, a third-party Java extension package needs to be called in a python project, which is also an important use of JPype.

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

 4. Example of setting system variables

system.setProperty(str(prop),str(value))

 5. Get system variable instance

value = system.getProperty(str(prop))

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325259972&siteId=291194637