Jar Package Download
<dependency> <groupId>jython</groupId> <artifactId>jython</artifactId> <version>2.1</version> </dependency>
Instance
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 afternoon 1:05:36 *<pre> <dependency> <g Roupid>jython</groupid> <artifactId>jython</artifactId> <version>2.1</version> & lt;/dependency> *</pre> * Desc: ... */public class Pythonbyjava {public static void main (Strin G[] args) {Pythoninterpreter interpreter = new Pythoninterpreter (); 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 ();