Call python in Java

Source: Internet
Author: User

Before running the command, you must load the corresponding Jython package. This is required.

 

1. Execute Python statements directly in the Java class

import javax.script.*;import org.python.util.PythonInterpreter;import java.io.*;import static java.lang.System.*;public class FirstJavaScript{ 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];");     }//main}

The result is Tue, which is displayed on the console and called directly.

2. Call the functions in the local Python script in Java:

First, create a Python script named my_utils.py.

def adder(a, b):    return a + b

Create a Java class for testing,

Java code firstjavascript:

import javax.script.*;import org.python.core.PyFunction;import org.python.core.PyInteger;import org.python.core.PyObject;import org.python.util.PythonInterpreter;import java.io.*;import static java.lang.System.*;public class FirstJavaScript{public static void main(String args[]){PythonInterpreter interpreter = new PythonInterpreter();interpreter.execfile("C:\\Python27\\programs\\my_utils.py");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());}//main}

The result is: ansible = 2012

3. Use Java to directly execute the Python script,

Create script inputpy

#open filesprint 'hello'number=[3,5,2,0,6]print numbernumber.sort()print numbernumber.append(0)print numberprint number.count(0)print number.index(5)

Create a Java class and call this script:

import javax.script.*;import org.python.core.PyFunction;import org.python.core.PyInteger;import org.python.core.PyObject;import org.python.util.PythonInterpreter;import java.io.*;import static java.lang.System.*;public class FirstJavaScript{ public static void main(String args[]) {    PythonInterpreter interpreter = new PythonInterpreter();  interpreter.execfile("C:\\Python27\\programs\\input.py"); }//main}

The result is:

hello[3, 5, 2, 0, 6][0, 2, 3, 5, 6][0, 2, 3, 5, 6, 0]23

 

 

 

 

 

 

 

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.