Personally effective --- python calls jar

# - * - Coding: UTF-8 - - * 
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 where
aa = jarpath + '\ JpypeDemo.jar'
Print (AA)
jvm_path jpype.getDefaultJVMPath = () # Get the current path of the system JDK
Print (jvm_path)
# startJVM ( "/ usr / local / Java / jdk1.8.0_181 / JRE / lib / AMD64 /server/libjvm.so","-ea","-Djava.class.path=%s "% (+ jarpath '/JpypeDemo.jar'))          
# # this startJVM function role is to load the Java virtual machine, its a parameter must be the installation location of your Java jdk, everyone is not the same installation location,
# my address is bold above that period (Note !!! some say the tutorials directly through getDefaultJVMPath () function get Java address
# is not recommended! not recommended! not recommended! because the address of this function is likely to be obtained computer comes with oracle version of Java,
# is not our own Java installed, this problem due to environmental variables It will lead to error);! paraphrase the second argument is unknown, anyway, plus on the right; 
# The third parameter is where you packaged jar package absolute path, I can see the path to the current directory and JpypeDemo.jar previously been combined.
jpype.startJVM (jvm_path, "- EA", "- Djava.class.path% S ="% AA)
JDClass JClass = ( "demo1.JpypeDemo") a Java application # class (magic ~)

JD = JDClass
Jprint = Java .lang.System.out.println # Java application output function output class
jprint (jd.sayHello ( "waw") ) # call the class's sayHello function, and print output function with Java Java return value
jprint (str (jd. calc (2,4))) # call the class of summing function, and print output function with Java Java return value to be printed as a character type of the value of

# close the Java virtual machine, you can write from time to write, do not write in the program will be automatically shut down at the end of
shutdownJVM ()

Guess you like

Origin www.cnblogs.com/longyi2020/p/12198438.html