Since python itself is a scripting language, and there are often cases of calling third-party libraries, sometimes using java to call python is not as convenient as using python to call java. Here is a summary of what operations are needed in the process of calling java from python. (The default is called on linux Ubuntu)
First of all, the jdk on linux must be installed. This process is not shown here. Python comes with linux, and the default here is python2.7.
Install jpype
Python calls java mainly by import jpype in python.
The installation of jpype is extremely easy. Download JPype-0.5.4.2.zip from http://jpype.sourceforge.net/ and place it in any folder. I put it under /home/UsrName/jpype/, Then under this folder:
unzip Jpype-0.5.4.2.zip
cd Jpype-0.5.4.2
python setup.py install
The installation is complete (if there is a Permission denied error, please change the last command to sudo python setup.py install), and you can execute it in python at this time:
# python
import jpype
Jpype installation is complete.
Use Jpype to call Java
Let's simply call a custom function in java as an example:
First attach a simple java code, the function is to return the processed string for a given string, and return the sum of two numbers given two parameters.
publicclassJpypeDemo{publicstatic String sayHello(String user){//note! As the interface function called by python, it needs to be static, otherwise python
End will report an error
return"hello"+ user;}publicstatic int calc(int a, int b){//note! As the interface function called by python, it needs to be static, otherwise python
End will report an error
return a + b;}publicstaticvoidmain(String[] args){}}
Package it as a jar file. Here I named the packaged file JpypeDemo.jar (please ask Baidu for those who won’t be packaged), and place it in the directory where the python script is located.
Then give the code of using java jar package on the python side:
import jpype
from jpype import*import os.path
jarpath = os.path.abspath('.') #This function is used to get the absolute path of the current python script
startJVM("/usr/local/java/jdk1.8.0_181/jre/lib/amd64/server/libjvm.so","-ea","-Djava.class.path=%s"%(jarpath +'/JpypeDemo.jar'))
The function of this startJVM function is to load the Java virtual machine. The first parameter must be the installation location of your Java jdk. The installation location of each person is different. My address is the section in bold above (note!!! Yes) The tutorial says that you can get the Java address directly through the getDefaultJVMPath() function, not recommended! Not recommended! Not recommended! Because the address obtained by this function is likely to be the oracle version of Java that comes with the computer, not our own installation Java, this will cause an error due to environmental variables!); The second parameter is unclear, so add it anyway; the third parameter is the absolute path of your packaged jar package, you can see that I will Combine JpypeDemo.jar with the path of the current directory obtained earlier
JDClass =JClass("JpypeDemo") #Apply for a Java class (magic~)
jd = JDClass
jprint = java.lang.System.out.println #Apply for the output function of the Java output class
jprint( jd.sayHello(" waw ")) #Call the sayHello function in this class, and use the Java output function to print the Java return value
jprint( jd.calc(2,4)) #Call the sum function in this class, and use the Java output function to print the Java return value
# Close the Java virtual machine, you can write or not, if you do not write, it will be automatically closed at the end of the program
shutdownJVM()
Execute the above program and get the output:
hello waw
6
JVM activity report:
classes loaded: 32
JVM has been shutdown
Content expansion:
Python calls java jar package method
from jpype import*
jvmPath =getDefaultJVMPath()
jars =["./Firstmaven-1.0-SNAPSHOT-jar-with-dependencies.jar"]jvm_cp ="-Djava.class.path={}".format(":".join(jars))startJVM(jvmPath,jvm_cp)
sedisObj =JClass("LogBack")
so =sedisObj()
print so.get_v('name0')
print so
print so.get_int()shutdownJVM()
So far this article on how python calls java classes is introduced. For more related python methods of calling java classes, please search for ZaLou.Cn's previous articles or continue to browse related articles below. Hope you will support ZaLou more in the future. Cn!
Recommended Posts