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