Calling the Python method in Java

Source: Internet
Author: User

1, http://sourceforge.net/projects/jython/Download the Jython package, add the Jython.jar to the project directory

Example:

1. Excerpt from: http://blog.csdn.net/anbo724/article/details/6608632

1. Execute python statements directly in the Java class

  1. Import javax.script.*;
  2. Import Org.python.util.PythonInterpreter;
  3. Import java.io.*;
  4. Import static java.lang.system.*;
  5. public class Firstjavascript
  6. {
  7. public static void Main (String args[])
  8. {
  9. Pythoninterpreter interpreter = new Pythoninterpreter ();
  10. Interpreter.exec ("days=" (' MoD ', ' Tue ', ' Wed ', ' Thu ', ' Fri ', ' Sat ', ' Sun ');
  11. Interpreter.exec ("Print days[1];");
  12. }//main
  13. }


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

    1. def adder (A, b):
    2. return a + b

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

Java Class Code Firstjavascript:

  1. import javax.script.*;
  2.   
  3. import org.python.core.PyFunction;
  4. import Org.python.core.PyInteger;
  5. import Org.python.core.PyObject;
  6. import Org.python.util.PythonInterpreter;
  7.   
  8. import java.io.*;
  9. import static java.lang.system.*;
  10. Public class Firstjavascript
  11. {  
  12. Public static void Main (String args[])
  13.     {  
  14.           
  15. Pythoninterpreter interpreter = new Pythoninterpreter ();
  16. interpreter.execfile ("c:\\python27\\programs\\my_utils.py");
  17. pyfunction func = (pyfunction) interpreter.get ("Adder", Pyfunction.class);
  18.   
  19. int a =, b = 2;
  20. Pyobject pyobj = func.__call__ (New Pyinteger (a), new Pyinteger (b));
  21. System.out.println ("anwser =" + pyobj.tostring ());
  22.   
  23.   
  24. }//main
  25. }  

The resulting result is: Anwser = 2012

3. Execute the Python script directly using Java,

Create a script inputpy

    1. #open Files
    2.   
    3. print ' Hello '
    4. number=[3,5,2,0,6]
    5. Print number
    6. Number.sort ()
    7. Print number
    8. number.append (0)
    9. Print number
    10. print number.count (0)
    11. Print Number.index (5)


To create a Java class, call this script:

  1. import javax.script.*;
  2.   
  3. import org.python.core.PyFunction;
  4. import Org.python.core.PyInteger;
  5. import Org.python.core.PyObject;
  6. import Org.python.util.PythonInterpreter;
  7.   
  8. import java.io.*;
  9. import static java.lang.system.*;
  10. Public class Firstjavascript
  11. {  
  12. Public static void Main (String args[])
  13.  {  
  14.     
  15. Pythoninterpreter interpreter = new Pythoninterpreter ();
  16. interpreter.execfile ("c:\\python27\\programs\\input.py");
  17. }//main
  18. }  


The resulting results are:

    1. Hello
    2. [3, 5, 2, 0, 6]
    3. [0, 2, 3, 5, 6]
    4. [0, 2, 3, 5, 6, 0]
    5. 2
    6. 3


Share:

Calling the Python method in Java

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.