Call the python method in java

Source: Internet
Author: User
Sourceforge. netprojectsjython download the jython package and put jython in it. jar to the project directory 1. directly execute the python statement viewplainimportjavax in the java class. script. *; importorg. python. util. pythonInterpreter; importjava. io. *; imports... 1. directly execute python statements in java classes

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.exe c ("days = ('mod', 'tue ', 'Wed', 'Thu', 'Fri', 'sat, 'sun ');");
Interpreter.exe c ("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.

View plain
Def adder (a, B ):
Return a + B

Create a java class for testing,

Java 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.exe cfile ("C :\\ Python27 \ programs \ my_utils.py"); // path, script name
PyFunction func = (PyFunction) interpreter. get ("adder", PyFunction. class); // adder python function name
Int a = 2010, B = 2;
PyObject pyobj = func. _ call _ (new PyInteger (a), new PyInteger (B); // pass the value, a B
System. out. println ("ansible =" + pyobj. toString ());
} // Main
}

The result is: Ansible = 2012

3. use java to directly execute the python script,
Create 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)

Create a java class and 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.exe cfile ("C :\\ Python27 \ programs \ input. py ");
} // Main
}

The result is:

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.