Jython-java Operations Python Script learning notes (ii) __ios

Source: Internet
Author: User
Tags python script

here is a record of using the Pydev plug-in to develop Python in eclipse while using Jython to enable Java and Python interoperability :

(a) about downloading the installation and configuring Pydev Plug-ins and Jython:

First in the official website download to the plug-in compression package http://pydev.org/, after decompression directly will plugins and features for overwriting the Eclipse installation directory files. Open Eclipse in your preferences, you can see that the Pydev is configured, and if it is not successful, your Eclipse version may be too high, you can see the relevant requirements on the website, and then download the low version of Eclipse to continue to load the installation manually, Xu Du website on the implementation of dynamic installation Plug-ins in Eclipse, feel the speed is not fast, also give up this way.

The above is successful after the relevant schematic diagram.

You can see that there are several configurations of the environment, such as Interactive-python, Interative-jython, and so on, if you are developing pure Python, You can load the PYTHON33 interpreter Python.exe installed in the previous section into this path to run the Python project you created:

The specifics of pure Python project development is simple, in Eclipse, new Python project can create the corresponding py file, and the right key run as Select Python can achieve rapid development.

(ii), about Jython installation and development testing in Eclipse

Jython is a Python Java implementation, Python is an efficient dynamic scripting language, can implement a variety of current popular languages, such as C, Java, and so on, and the following is to build a Python Java implementation of the environment:

1, the official website downloads to http://jython.org/installs the package Jython-installer-2.5.3.jar, as long as has the virtual machine environment to be possible to click to run directly, it is the pure Java realizes one environment, After installation you can see that the installation directory structure is similar to the original Python, built like the JDK installation can also create an environment variable easy to use, such as: jython-home, path and so on. The installation directory Lib package is a few fixed functions of the Python library, One more Jython.jar package, this package is Java invoke Python API, which has a more common class is: Org.python.util.PythonInterpreter, the following description of a simple call:

As a simple step, create a new project in Eclipse, build the Jython.jar package path in the project, and the following is the calling code:

Import Org.python.util.PythonInterpreter; 
Import org.python.core.*;  
public class Test {public     
        static void main (String []args)        throws Pyexception    
        {         pythoninterpreter Interp =new pythoninterpreter ();
                  System.out.println ("Hello, Brave New World");
                  Interp.exec ("Import sys");
                  Interp.exec ("Print sys");
                  Interp.set ("A", new Pyinteger);        
                  Interp.exec ("Print a");        
                  Interp.exec ("x = 2+2");        
                  Pyobject x = Interp.get ("x");         
                  System.out.println ("x:" +x);        
                  System.out.println ("Goodbye, Cruel World");    
        }


The results of the operation are as follows:

<module ' sys ' (built-in) >
42
X:4
Goodbye, Cruel World

A little bit of Python should know that this script calls OK in Java. Of course, if we don't write Python code directly in the Exec () method, we can also directly manipulate the written Python file in Java:

Pythoninterpreter interp = new Pythoninterpreter ();

Interp.execfile ("src/h.py");

Direct call is OK.

2, since Jython is Java and Python interactive language, the above has called the Python script in Java, the following simple implementation of Python use JDK library:

To implement the above call, the prerequisite is to set up the Jython and Java environment variables (because it invokes the Java library function). Can directly in CMD to the directory where the py file, run Jython simple.py can run the

The following example is in the Jython installation directory Demo:

From PAWT import swing
import Java

def exit (E): Java.lang.System.exit (0)

frame = swing. JFrame (' Swing Example ', visible=1)
button = Swing. JButton (' Close me! ', actionperformed=exit)
frame.contentPane.add (button)
frame.pack ()


The result of running in CMD is conceivable:

It's simpler to be in eclipse, as long as you've configured the environment in a new Python project

Create a new Python file under that project: the same as above: direct right button run as-->jython run has the result.

Personal research is over, record it as a share, and later have a chance to reach deeper content to be recorded later ...

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.