Python
  Home arrow Python arrow Page 4 - Introduction to Jython
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

Introduction to Jython
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 21
    2005-02-22

    Table of Contents:
  • Introduction to Jython
  • Installing Jython
  • Calling Java Classes
  • Embedding

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    Introduction to Jython - Embedding
    (Page 4 of 4 )

    Jython allows us to embed Python code in Java code easily, and there are several approaches to it. The first is to use the PythonInterpreter object to execute Python code contained in a separate file. Let's create a Python file called "testCode.py" with the following code inside of it:

    import math
    print 'Hello Jython World!'
    print math.pi
    print math.e
    print math.sqrt ( 25 )

    Execuing it in Java is simple using the execfile method:

    import org.python.util.PythonInterpreter;
    import org.python.core.*;
    class TestPython {
       public static void main ( String[] args ) {
          try {
             org.python.util.PythonInterpreter python = new org.python.util.PythonInterpreter();
             python.execfile ( "testCode.py" );
          } catch ( Exception e ) {
             System.out.println ( "An error was encountered." );
          }
       }
    }

    We can also place code within the Java application itself using the exec method. Let's recreate our first example with the Python code included in the Java application rather than an external file:

    import org.python.util.PythonInterpreter;
    import org.python.core.*;
    class TestPythonTwo {
       public static void main ( String[] args ) {
          try {
             org.python.util.PythonInterpreter python = new org.python.util.PythonInterpreter();
             python.exec ( "import math" );
             python.exec ( "print 'Hello Jython World!'" );
             python.exec ( "print math.pi" );
             python.exec ( "print math.e" );
             python.exec ( "print math.sqrt ( 25 )" );
          } catch ( Exception e ) {
             System.out.println ( "An error was encountered." );
          }
       }
    }


    We can also get and set variables in the local namespace by using the get and set methods. This allows us to interact with Python a bit more, which is our goal when embedding Python code inside Java applications:

    import org.python.util.PythonInterpreter;
    import org.python.core.*;
    class TestPythonTwo {
       public static void main ( String[] args ) {
          try {
             org.python.util.PythonInterpreter python = new org.python.util.PythonInterpreter();
             python.exec ( "import math" );
             python.exec ( "print 'Hello Jython World!'" );
             python.exec ( "print math.pi" );
             python.exec ( "print math.e" );
             python.set ( "ourSetVariable", new org.python.core.PyInteger ( 25 ) );
             python.exec ( "ourSetVariable = math.sqrt ( ourSetVariable )" );
             org.python.core.PyObject ourSetVariable = python.get ( "ourSetVariable" );
             python.exec ( "print " + ourSetVariable );
          } catch ( Exception e ) {
             System.out.println ( "An error was encountered." );
          }
       }
    }

    Conclusion

    Jython is an extremely powerful tool for development in both Python and Java. It allows you to use Python to call Java classes, increasing the power of Python, and it allows you to execute Python code inside Java applications by using the exec and execfile methods. Jython also allows you to subclass Java classes using Python. Jython can significantly decrease the effort needed to produce a powerful application, or even an applet, servlet or bean.

    Of course, this article only covers the tip of the iceburg (forgive the cliché!). There is a lot more to Jython, but it would take a very lengthy article to cover all of it. It's up to you to explore more of Jython. Good luck!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Nice =) Great subject, well written as usual
       · Is this real? Jython?
       · If you had read the article I don't think you'd be asking that question, but yes, it...
       · Thanks, David!I wrote this article because I find Jython extremely interesting....
       · I like the idea. I like python, especially for quick and dirty Unix tasks, like...
       ·  If you want to read how, see this...
       · Well, programming languages typically derive from the desire to complete a certain...
     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway