Java invoke Jython execute Python file

Source: Internet
Author: User
Tags exit in

In web development, you often use the local environment's Third-party libraries for the Web environment, and this article explains how Java executes Python files.
Online said there are three kinds of methods, in fact, there are two kinds, the following introduction of the second Pass (Jython). Method One Java.lang.Runtime

    Runtime RT = Runtime.getruntime ();
        try {
               Process proc = rt.exec ("python/tmp/test.py");
        } catch (Exception e) {
            e.printstacktrace ();
        }

Subtotal:
1. Runtime.getruntime () can get the runtime environment of the current JVM, which is the only way to get a run-time environment in Java.
2. Most of the other methods on runtime are instance methods, which means that the GetRuntime method is used every time a run-time call is made.
3. The exit method in runtime is the way to exit the current JVM and is estimated to be the only one, because I see that exit in the system class actually exits the JVM by calling Runtime.exit (). Here is a description of the Java runtime return value of the general rules (also mentioned below), 0 for normal exit, not 0 for abnormal abort, this is only the Java rules, in each operating system will always occur some small confusion. second (emphasis) calling the Jython API

First step: Adding dependencies

<!--Https://mvnrepository.com/artifact/org.python/jython-->
    <dependency>
      <groupId> org.python</groupid>
      <artifactId>jython</artifactId>
      <version>2.7.0</ Version>
    </dependency>

Step Two: Create a new Test.java test class

Import Org.python.util.PythonInterpreter;

Import java.util.Properties;

/**
 * Author: Meet small star
 * email:tengxing7452@163.com
 * date:17-3-21 * Time
 : PM 8:18
 * 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 Three: Run Test.java

Testing started at afternoon 9:40
... Tue this are
test.py
created by tengxing on 2017.3!

The process has ended, exit code 0

Reminders may report the following exceptions:

Exception in thread "main" importerror:cannot the Import site module and its dependencies:no module named site
determine If the following attributes are correct:

Reason: no initialization of Python.import.site
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

    Invokes the method in Python and prints the result
    pyfunction func = (pyfunction) interpreter.get ("Adder", pyfunction.class);
    int a =, b = 2;
    Pyobject pyobj = func.__call__ (New Pyinteger (a), new Pyinteger (b));
    System.out.println ("Anwser =" + pyobj.tostring ());

Reference articles:
http://blog.csdn.net/fei33423/article/details/53491414
http://blog.csdn.net/guo_rui22/article/details/3765378
Http://www.cnblogs.com/liinux/p/5481849.html
http://www.oschina.net/code/snippet_119671_5971

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.