Python
  Home arrow Python arrow Page 4 - Introduction to Jython
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PYTHON

Introduction to Jython
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    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!



     
     
    >>> More Python Articles          >>> More By Peyton McCullough
     

       

    PYTHON ARTICLES

    - Tuples and Other Python Object Types
    - The Dictionary Python Object Type
    - String and List Python Object Types
    - Introducing Python Object Types
    - Mobile Programming using PyS60: Advanced UI ...
    - Nested Functions in Python
    - Python Parameters, Functions and Arguments
    - Python Statements and Functions
    - Statements and Iterators in Python
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek