Python calls Java instances and pythonjava instances
Details about calling Java instances using Python
Preface:
Python programming on the server is not as good as Java. Therefore, Java code may be called.
Prerequisites:
Linux environment
1 install jpype1
Test code after installation:
from jpype import *startJVM(getDefaultJVMPath(), "-ea")java.lang.System.out.println("Hello World")shutdownJVM()
2. Call a non-jdk jar package, test. jar
The package contains the com. Test class.
package com;public class Test { public String test(String str){ return str; }}
Python calls the jar package
Jar_path = OS. path. join (OS. path. abspath ('. '), 'libs/test. jar ') jpype. startJVM (jpype. getDefaultJVMPath (), "-ea", "-Djava. class. path = % s "% jar_path) Test = jpype. JClass ('com. test') # Or use JPackage to reference the Test class # com = jpype. JPackage ('com ') # Test = com. testt = Test () res = t. test ("a") print resjpype. shutdownJVM ()
Note: Pay attention to Linux permissions.
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!