This article describes the Java implementation method of calling jython to execute python files. Share it for your reference, as follows:
During web development, you often use third-party libraries in the local environment in the web environment. This article explains how Java executes python files.
It is said online that there are three methods, but in fact there are only two methods. The second method is introduced below (jython).
Method 1
java.lang.Runtime Runtime rt = Runtime.getRuntime(); try { Process proc = rt.exec("python /tmp/test.py"); }catch (Exception e){ e.printStackTrace(); }Here is a sub-conception:
1. Runtime.getRuntime() can obtain the runtime environment of the current JVM, which is also the only way in Java to get the runtime environment.
2. Most other methods on Runtime are instance methods, which means that getRuntime method must be used every time the runtime call is made.
3. The exit method in Runtime is a method to exit the current JVM, which is probably the only one. Because I saw that exit in the System class actually exits the JVM by calling Runtime.exit() . Here I will explain the general rules of Java for the return value of Runtime (also mentioned later). 0 represents normal exit, and non-0 represents abnormal abortion. This is just a Java rule. There will always be some minor confusion in each operating system.
The second type (key point)
Calling jython API
Step 1: Add dependencies
<!-- https://mvnrepository.com/artifact/org.python/jython --> <dependency> <groupId>org.python</groupId> <artifactId>jython</artifactId> <version>2.7.0</version> </dependency>
Step 2: Create a new Test.java test class
import org.python.util.PythonInterpreter;import java.util.Properties;/** * Author: Meet Xiaoxing* Email: [email protected] * Date: 17-3-21 * Time: 8:18 pm * Describe: jpython test */public class Test { public static void main(String []args){ PythonInterpreter interpreter = new PythonInterpreter(); interpreter.exec("days=('Mod','Tue','Wed','Thu','Fri','Sat','Sun'); "); interpreter.exec("print days[1];"); interpreter.execfile("/tmp/test.py"); interpreter.exec("print 'created by tengxing on 2017.3'"); }}
Step 3: Run Test.java
Testing started at 9:40 pm ...Tuethis is test.pycreated by tengxing on 2017.3!
The process has ended, exit code 0
The reminder may report the following exception:
Exception in thread "main" ImportError: Cannot import site module and its dependencies: No module named site
Determine if the following attributes are correct:
Reason: python.import.site is not initialized
solve:
public class Test { public static void main(String []args){ Properties props = new Properties(); props.put("python.home", "path to the Lib folder"); props.put("python.console.encoding", "UTF-8"); props.put("python.security.respectJavaAccessibility", "false"); props.put("python.import.site", "false"); Properties preprops = System.getProperties(); PythonInterpreter.initialize(preprops, props, new String[0]); PythonInterpreter interpreter = new PythonInterpreter(); interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); "); interpreter.exec("print days[1];"); interpreter.execfile("/tmp/test.py"); interpreter.exec("print 'created by tengxing on 2017.3!'"); }}OK Perfect
//Call the method in python and print the result PyFunction func = (PyFunction) interpreter.get("adder",PyFunction.class);int a = 2010, b = 2;PyObject pyobj = func.__call__(new PyInteger(a), new PyInteger(b));System.out.println("anwser = " + pyobj.toString());Reference article:
//www.VeVB.COM/article/137380.htm
//www.VeVB.COM/article/137385.htm
Attachment: jython.jar Click here to download this site .
For more Java-related content, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.