Calling the Python method in Java

Source: Internet
Author: User
1. Execute python statements directly in the Java class

View Plain
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 in the console, which is called directly.

2. Call the functions in the native Python script in Java:
First, create a python script named: my_utils.py

View Plain
Def adder (A, B):
Return a + b

Then build a Java class that you can use to test,

Java Class Code Firstjavascript:

View Plain
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");//path, script name
pyfunction func = (pyfunction) interpreter.get ("Adder", pyfunction.class);//adder python function name
int a =, b = 2;
Pyobject pyobj = func.__call__ (New Pyinteger (a), new Pyinteger (b));//pass value, a B
System.out.println ("Anwser =" + pyobj.tostring ());
}//main
}

The resulting result is: Anwser = 2012

3. Execute the Python script directly using Java,
Create a script inputpy

View Plain
#open files

print ' Hello '
number=[3,5,2,0,6]
Print number
Number.sort ()
Print number
Number.append (0)
Print number
Print Number.count (0)
Print Number.index (5)

To create a Java class, call this script:

View Plain
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 resulting results are:

View Plain
Hello
[3, 5, 2, 0, 6]
[0, 2, 3, 5, 6]
[0, 2, 3, 5, 6, 0]
2
3
  • 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.