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
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.
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)
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