Jython and Java

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.

 

Code:

 

Fibo. py

Bytes --------------------------------------------------------------------------------------------

# Fibonacci numbers Module

Def fib (n): # Write Fibonacci series up to n

A, B = 0, 1

While B <n:

Print B,

A, B = B, A + B

Def 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

Bytes ------------------------------------------------------------------------------------------------------

 

Testjython. Java

 

Bytes ----------------------------------------------------------------------------------------------------------

Package platform. OAAS. ******. Other. test;

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 Jython in the following aspects:
* 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 add [./pythonsrc] to the module search path.
Interp.exe C ("Import sys ");
Interp.exe C ("SYS. Path. append ('./pythonsrc ')");

// 2. Introduce the fibo, execute the fibo. Fib (100), OK
Interp.exe C ("Import fibo ");
Interp.exe C ("Maid (100 )");

// 3. Get the variable value of the python built-in type 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 ();
}
}
Bytes ------------------------------------------------------------------------------------------------------------------------

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.