Python calls Java instances and pythonjava instances

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.