Home >Backend Development >Python Tutorial >Detailed explanation of Python's sample code for calling Java
This article mainly introduces Python Detailed explanation of calling Java instances. Friends in need can refer to
Detailed explanation of Python calling Java instances
Foreword:
Python is not as good as Java for server-side programming, so Java code may need to be called in this regard
Prerequisite:
Linux Environment
1 Installation jpype1
Test code after installation :
from jpype import * startJVM(getDefaultJVMPath(), "-ea") java.lang.System.out.println("Hello World") shutdownJVM()
2 Call the 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') # 或者通过JPackage引用Test类 # com = jpype.JPackage('com') # Test = com.Test t = Test() res = t.test("a") print res jpype.shutdownJVM()
note: Pay attention to permission issues under Linux
The above is the detailed content of Detailed explanation of Python's sample code for calling Java. For more information, please follow other related articles on the PHP Chinese website!