Mutual calls between Python and Java--jython

Source: Internet
Author: User

Overview:

Jython is a tool that can combine two different programming languages. First, it enables Python programmers to get involved in the Java development environment and enable them to quickly develop schema prototypes for seamless integration into existing Java platforms. Second, it can simplify the work of millions of Java programmers by adding scripting languages to Java. Java programmers don't have to write a whole bunch of test cases or drivers for a class they just wrote. Jython provides most of the functionality of Python, as well as the ability to instantiate Java classes and interact with Java classes. The Jython code is dynamically compiled into Java bytecode, so you can extend the Java class with Jython. You can also use Java to extend Python. It is easy to write a class in Python that uses this class like a Java class. You can even compile the Jython script statically to Java bytecode.

--"Python core programming"

My environment:

System: CentOS release 6.5 (Final)

Jdk:java Version "1.7.0_75"

Python:python 2.6.6

Jython:2.7-b1


Installation:

Jython installation package Address (Jython-installer-2.7-b1.jar):

http://download.csdn.net/detail/u013761665/8590143


Installation steps:

1. Switch the working directory to the downloaded Jython-installer-2.7-b1.jar directory (of course, if you use some relative or absolute path is also possible );

2. Install using the Java-jar Jython-installer-2.7-b1.jar command ( This step is based on the successful installation of the JDK in your system);

3. go to the Jython installation directory and enter the./jython. Note that you cannot use Jython here unless you have configured your path


Java code writing:

Package Com.w.shell.jar.main;public class Main {public    main () {        System.out.println ("Hello, Welcome this shell.") ;    }        /**     * @author Q-whai     * 2015-4-13 * * Public    static void Main (string[] args) {        new Main ()    }    public static void Printlog () {        System.out.println ("This is a shell log show 2. And you'll get nothing whit this function return. ");        public static string GetLog () {        string toreturn = "Custom log";        System.out.println ("This is another shell log show 2. And you'll get a return String. ");        return toreturn;}    }


jar Package Production:

Manipulate the jar through the Java code above. However, the operation of the jar package is no longer described too much, since in my early blog there is a detailed description (using instructions in cmd to execute the jar package).


Jython Code writing:

The code for Jython is very simple, as follows:

Import Syssys.path + = ["/root/study/coding/java/shell.jar"]from com.w.shell.jar.main import Mainmain.printlog () result = Main.getlog () print result


To Run the example:

1. Switch the working directory to the installation directory of Jython (of course, if you are using some relative or absolute path );

2. Run: /.. /splace/jython/jython jython_test.py (i.e.: Your_jython_path your_jython.py)

3. Operating effect:



Mutual calls between Python and Java--jython

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.