Use Runtime.getruntime (). exec () to invoke the Python script in Java

Source: Internet
Author: User
Tags python script

For example, there is a Python script called test.py, which now wants to invoke this script in Java. Assuming that the test.py inside uses the extended package, making the embedded compiler such as Pythoninterpreter unusable, then only Java calls the console process, that is, Runtime.getruntime (). EXEC (), To run this Python script.

Under Windows to run this program, if it is a reference to some Baidu has been reproduced countless times of the article, it is possible to run the result is not the execution of this script at all. After testing, execute the following code in Java to successfully run the test.py file:

--------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------
Process proc = Runtime.getruntime (). EXEC ("cmd/c python " + filepath + "test.py"); //FilePath is the address of test.py. Relative addresses can be taken to the root of the project site
Proc.waitfor ();

--------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------
The Python output window will not pop up when using CMD/C, you can use the cmd/k start prefix to pop up a new console window and display the Python output


cmd/c dir closes the command window after executing the dir command.
cmd/k dir does not close the command window after executing the dir command.

cmd/c start dir will open a new window and execute the dir instruction, the original window will be closed.
cmd/k start dir will open a new window and execute the dir instruction, the original window will not be closed.

Use Runtime.getruntime (). exec () to invoke the Python script in Java

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.