<dependency> <groupId>jython</groupId> <artifactid >jython</artifactId> <version>2.1</version> </dependency>
Package Com.curiousby.python.demo;import Org.python.core.pyfunction;import Org.python.core.pyinteger;import Org.python.core.pyobject;import org.python.util.pythoninterpreter;/** * @author baoyou e-mail:curiousby@163.com * @ Version August 1, 2016 PM 1:05:36 *<pre> <dependency> <groupId>jython</groupId> < artifactid>jython</artifactid> <version>2.1</version> </dependency> *</pre> * Desc: ... */public class Pythonbyjava {public static void main (string[] args) {Pythoninterpreter interpreter = new Pytho Ninterpreter (); Interpreter.execfile ("i:\\cache\\ea-ws\\demojava\\conf\\test1.py"); pyfunction func = (pyfunction) interpreter.get ("sum", pyfunction.class); int a = 1, b = 2; Pyobject pyobj = func.__call__ (New Pyinteger (a), new Pyinteger (b)); System.out.println (Pyobj.tostring ()); }}
Results:
3
The other two methods of invocation are:
A) Execute Python script code directly
Pythoninterpreter interpreter = new Pythoninterpreter (); Interpreter.exec ("days=" (' MoD ', ' Tue ', ' Wed ', ' Thu ', ' Fri ', ' Sat ', ' Sun ') ; Execute Python Script
b) Execute script file using Runtime.getruntime ()
Process proc = Runtime.getruntime (). EXEC ("Python d:\\demo.py"); Proc.waitfor ();