Home > Backend Development > Python Tutorial > Detailed explanation of Python's sample code for calling Java

Detailed explanation of Python's sample code for calling Java

黄舟
Release: 2017-06-04 10:09:03
Original
1714 people have browsed it

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()
Copy after login

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;
  }
}
Copy after login

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()
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template