I. Introduction to JPype
What is 1? JPype?
JPype is a tool that allows python code to call Java code conveniently, thus overcoming python's shortcomings in some areas, such as server-side programming.
2.2 What's the difference? JPype and Jython (successor Jython(JPython)?
1) The running environment is different: jython runs on the jvm, while the actual running environment of JPype is still the python runtime, but an embedded JVM is started during the running process;
2) Different users: jython is for java programs and JPype is for python programmers.
Second, JPype installation.
1. Install Python2.7 and JAVA 1.6 first.
2. Install JPYPE-0.5.4.2.win32-py2.7.exe (/projects/JPYPE/files/JPYPE/0.5.4/).
3.Ubuntu 12.04 installation command: sudo apt-get install python-jpype.
Three. Instructions for use of JPype
1. Start JVM.
The function of startJVM () provided by JPype is to start the JAVA virtual machine, so this method must be called to start the JAVA virtual machine before calling any subsequent JAVA code.
The definition of jpype.startJVM ()
Parameters of startjvm (JVM, * args) jpype. startjvm()
Parameter 1: jvm describes the path of jvm.dll file in your system, such as "c: program files ibmjava50jreinj9vmjvm.dll". You can get the default JVM path by calling jpype.getDefaultJVMPath ().
Parameter 2: args, an optional parameter, will be directly passed to JVM by JPype as the startup parameter of Java virtual machine. This applies to all legal JVM startup parameters, such as:
-agentlib:libname[=options]
-classpath classpath
-Lengthy
-Xint 2。 Shut down the JVM.
When you have finished using the JVM, you can close the JVM through jpype.shutdownJVM (), which has no input parameters. When the python program exits, the JVM will automatically shut down.
3. Refer to the third-party Java extension package
It is often necessary to call the third-party Java extension package in python projects, which is also an important use of JPype.
By adding:-djava. class.path = ext _ classpath to the JVM startup parameter, you can call the existing java expansion package with python code.
4. Access the system properties of JAVA
Sometimes, some Java applications need to set or get system properties in the JVM.
Example of setting system variables at JVM startup:
Add the following parameters to the startup parameters of the JVM:
-d property = fourth value, for example.
1. Call JAVA API directly.
Import from jpype *
Import os.path
start JVM(" C:/Java/JDK 1 . 6 . 0 _ 10/JRE/bin/client/JVM . dll ","-ea ")
Java . lang . system . out . println(" hello World ")
Close JVM()2. Call JAVA third-party extension package.
1)JAVA custom third-party jar package: package the JpypeDemo class into a jpypedemo.jar file and store it in the F:/sample_Py directory.
Bao jpype
Public class JpypeDemo {
Common string sayHello (string user) (
Return to "Hello"+user;
}
public int calc(int a,int b){
Return a+b;
}
} 2)Python calls the third-party JAVA jar package program.
Import from jpype *
Import os.path
jar path = OS . path . join(OS . path . abspath(' . ')),' F:/sample_Py/')
start JVM(" C:/Java/JDK 1 . 6 . 0 _ 10/JRE/bin/client/JVM . dll "、"-ea "、"-djava . class . path = % s " %(jar path+' jpypedemo . jar '))
# start the JVM in #ubuntu ("/home/geek/Android/JDK1.6.0 _ 43/JRE/lib/i386/server/libjvm.so", "-ea", "-djava.class.path =% s"%.
JDClass = JClass("jpype。 JpypeDemo”)
jd = JDClass()
# jd = jpackage ("jpypepe ")。 Jpypedemo () # Two Ways to Create JD
jprint = Java . lang . system . out . println
jprint(jd.sayHello("waw "))
jprint(jd.calc(2,4))
Close JVM()3. Access the system properties of JAVA.
Suppose the property you want to set is called yourProperty and the property value is yourValue.
1) Example of setting system variables at JVM startup
Import jpype
JVM path = jpype . getdefaultjvm path()
JVM arg = "-DyourProperty = your value "
If it's not jpype.isJVMStarted ():
JYPE。 Startjvm (jvmpath, jvmArg)2) An example of setting system variables in a program.
Import jpype
Prop = "Your property"
Value = "Your value"
system = jpype。 JClass('java.lang.System ')
System. Setproperty (str (prop), str (value)) 3) gets examples of system variables in a program.
Import jpype
Prop = "Your property"
system = jpype。 JClass('java.lang.System ')
value = system . getproperty(str(prop))