Python calls Java instance detail

  • 2020-06-03 06:58:43
  • OfStack

Python calls Java instance detail

Preface:

Python does not program as much on the server side as Java, so you might want to call Java code for this

Premise:

Linux environment

1 installation jpype1

Test code after installation:


from jpype import *
startJVM(getDefaultJVMPath(), "-ea")
java.lang.System.out.println("Hello World")
shutdownJVM()

2 Calls the non-ES24en jar package, test.jar

The package contains the ES30en.ES31en 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 by JPackage reference Test class 
# com = jpype.JPackage('com')
# Test = com.Test
t = Test()
res = t.test("a")
print res
jpype.shutdownJVM()

note: Note the permissions under Linux

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: