Home arrow Python arrow Page 4 - Introduction to Jython

Embedding - Python

Java and Python -- each have their advantages and disadvantages. What is a programmer to do if he wants ease, efficiency, and power? Believe it or not, there is a solution -- a way to merge both languages and get the best of both worlds. Read on to find out how it works.

TABLE OF CONTENTS:
  1. Introduction to Jython
  2. Installing Jython
  3. Calling Java Classes
  4. Embedding
By: Peyton McCullough
Rating: starstarstarstarstar / 24
February 22, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- 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

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: