How does python call java classes

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

How does python call java classes
How does Python call cmd using OS modules
How does python output backslashes
How does python update packages
How does python call the key of a dictionary
How does python perform matrix operations
How does Python list update value
How does python change the environment
How to write classes in python
How does python import dependency packages
How does python enter interactive mode
How does Python generate xml files
How does python determine prime numbers
How does python improve the calculation speed
How does Python handle the json module
How does python distinguish return and yield
How does Python store data to json files
How does python get input examples from the keyboard
How python was invented
How does python handle the program cannot be opened
How does python judge the module installation is complete
CentOS Python Java installations
How Python parses XML
How long does it take to learn python by myself?
How to comment python code
How Python converts string case
How to learn python quickly
How to uninstall python plugin
Does Python support multiple inheritance?
How Python implements FTP function
How to understand python objects
How to use python tuples
python functions, classes, modules, packages
Explain how python references package package