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