Java calls Python program

Source: Internet
Author: User

Recently, there are two main implementations that need to use Java programs to invoke Python code in your application.

First, use the Jython rack package

Because JSON objects are needed in the calling Python code, starting with jython2.5.2 and discovering that JSON is not supported. So

Upgraded with the latest Jython2.7.0, found to be supported. Therefore, to use more Python libraries, a later version of the Jython package is recommended.

First, add Jython's maven dependency:

After that, a simple test program was written:

Thereafter, the following results can be obtained:

Second, execute the script file using Runtime.getruntime ()

This is the same way you call a python program in CMD using the python+ file name command.

Among them, the Python program is as follows:

The execution results are the same as the Jython execution results.

Java calls Python program

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.