Java calls Jython

Source: Internet
Author: User
 

 

The Python script needs to be called in the Java JVM process during the past two days. The script language is really awkward when I got down to Jython trainer. I used Python a few years ago when I wrote an automated testing tool, but I forgot about it. Okay, go straight to the topic.

Prerequisites:

Sun-jre1.6, Jython 2.5

2. In the next jython_installer-2.5.0.jar under the official website, all the way to next, in/Jython-install-path/There Is A Jython. jar, the Jython. Jar import into the Java project.

Python code: fibo. py

 # Fibonacci numbers moduledef fib(n):    # write Fibonacci series up to n    a, b = 0, 1    while b < n:        print b,        a, b = b, a+bdef fib2(n): # return Fibonacci series up to n    result = []    a, b = 0, 1    while b < n:        result.append(b)        a, b = b, a+b    return result

Java code: testjython. Java

Import Org. python. core. pyexception; import Org. python. core. pyfile; import Org. python. core. pyobject; import Org. python. core. pystring; import Org. python. util. pythoninterpreter; public class testjython {/*** pay attention to the following aspects of Jython: * 1. how to import a Jython module from a specified path? * 2. How to call the methods in the module? * 3. How should I transmit parameters between Java and Jython? ** @ Param ARGs * @ throws pyexception */public static void main (string [] ARGs) throws pyexception {pythoninterpreter interp = new pythoninterpreter (); // 1. // introduce the system module and set [. /pythonsrc] is added to the module search path. Interp.exe C ("Import sys"); interp.exe C ("sys. path. append ('. /pythonsrc ') "); // 2. introduce the fibo and execute the fibo. FIB (100), OK interp.exe C ("Import fiber"); interp.exe C ("fiber. FIB (100) "); // 3. obtain the python built-in variable values in the Java program. String text = "'This is a bad Day'"; interp.exe C ("x =" + TEXT + "+ '!!! '"); Interp.exe C (" Print X "); pyobject object = interp. get ("X"); pystring xstring = object. _ STR _ (); string outx = xstring. asstring (); interp.exe C ("_ file = open ('/home/Fore/work/oaassearch/search/oaas_search/workspace/oaas-search0.1/pythonsrc/fibo. PY ') "); object = interp. get ("_ file"); pyfile file = (pyfile) object; pystring content = file. read (100); system. out. println (content); interp.exe C ("array = _ file. read (100) "); String Array = interp. get ("array "). asstring (); system. out. println (array); interp.exe C ("_ file. close () "); file. close ();}}

In this way, the call can be completed, and the Code has not been fully tested. After the test is passed, further adjustments will be made!

 

Related Article

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.