corner imagecorner image
FeaturesPluginsPlatformDocs & SupportCommunityPartners

Introduction to Jython Application Development Using NetBeans

Contributed by Josh Juneau. Edited by James Branam.

This document provides instructions for developing a Jython application using the Python plugin available in the NetBeans IDE. The tutorial shows you how to configure your Python installation by setting up both Python and Jython paths and manipulating module locations. This tutorial is not intended to teach the Python/Jython programming language, but rather demonstrate how the NetBeans IDE can be used with Python/Jython-based applications.

You need the Python EA download of the NetBeans IDE or the Python plugins installed to complete this tutorial.

Expected duration: 20 minutes

Contents

Content on this page applies to NetBeans IDE 6.7

To follow this tutorial, you need the following software and resources.

Software or Resource Version Required
NetBeans IDE 6.5 or 6.7 Python EA Version
Java Developer Kit (JDK) Version 6 or version 5
Jython Version Jython 2.5b0+

Overview of the Application

In this tutorial, you create a simple RSS reader with Jython, an implementation of the Python programming language written in Java.

Setting Up

Before you create your application, make sure that your IDE contains Python/Jython functionality. You can download the Python Early Access (EA) version of the IDE, or you can install the NetBeans Python plugin in the Plugin Manager. The NetBeans Python plugin is easy to install and configure. After installation, the IDE detects your various environments. You can configure the IDE for Python/Jython support in the Python Platform Manager. This window allows you to add or remove Jython or Python installations available to the IDE. Using the Python Platform Manager, you can also set up and configure your Python and Java paths. In doing so, you can place Java jar files or Python modules in your path so that they are available to use each time you start up the IDE. To take it one step further, you can even define different Java or Python paths for each installation you configure.

Adding a New Jython or Python Installation

  1. In the main toolbar, choose Tools > Python Platforms Manager.

    The Python Platform Manager opens, as seen in the following image.

    Python Platform Manager
  2. If your Jython or Python installation does not appear by default, click New, browse to the installation you want to use and choose Open.

    The IDE add the installation to the the list of Platforms. If you are unsure of the location of the installation, click Auto Detect.

Installing and Configuring your Jython Installation

  1. Choose Tools > Python Installations to open the Python Platform Manager.
  2. Select the Jython installation you want to use with the IDE and click Make Default.
  3. Enter any command arguments for the installation in the Command Arguments text field.

    You can also rename the Platform name if necessary.

  4. Click Close to close the Python Platform Manager.

Creating the Project

Begin by creating a new Python project in the IDE. The New Project wizards contains built-in templates for numerous project types. You can access the New Project wizard in a number of ways. You can choose File > New Project in the main toolbar, or you can right-click in the Projects window and choose New project.

  1. In the main toolbar, choose File > New Project.

    The New Project wizard opens with Python displayed as the category.

    Note: Python is displayed as the project when only the Python EA version of the IDE has been installed on your machine. Other categories may appear if Python EA was added to the IDE as a plugin.

  2. Select Python Project as the Project Type, as seen in the following image. Click Next.

    New Project wizard with project type
  3. Enter FeedReader as the Project Name, as seen in the following image. Select the version of Jython you want to use. Leave the Set as Main Project checkbox and Create Main File checkbox selected.

    New Project Wizard with project name and location

    The IDE creates a project folder based on the name of the project. You can change the name and location of this folder.

  4. Select the Python Platform you want to use from the drop down list, then click Finish to create the project.

    The IDE creates the project. Note how your project is displayed in the Projects window.

    Projects window showing project

    Note that feedreader.py opens in the Source Editor of the IDE, displaying basic information, as seen in the following image. Netbeans IDE automatically documents the author and date of the project along with providing a short sample print "Hello" application.

    Initial view of Source Editor

Adding Code

In this section, you add Jython code to your new project. This code is a basic Python application that reads RSS feeds.

  1. Remove the existing code in feedreader.py in the Source Editor.

  2. Erase the original content of feedreader.py. Type the following code in the Source Editor.
    from java.net import URL
    from java.io import InputStreamReader
    from java.lang import Exception
    from java.lang import Object
    from com.sun.syndication.feed.synd import SyndFeed
    from com.sun.syndication.io import SyndFeedInput
    from com.sun.syndication.io import XmlReader

  3. The Source Editor should then resemble the following image. Note the yellow light bulbs to the left of each import statement. Each light bulb tells you that the import statement is not used in the application. When you reference the statement in the application, the light bulb disappears.

    Source Editor after removal of unneeded information
  4. Type the following class declaration in the Source Editor. Note how the IDE assists you by adding keywords such as self, and parentheses and colons where required.

    class FeedReader(Object):
    def __init__(self, url):
    self.inUrl = url

    The IDE also indent for you in most cases. However, you need to outdent whenever you add a code block. The Source Editor should now resemble the following image. Note that there is no yellow light bulb next to the java.lang import Object import statement.

    Source Editor with first part of code
  5. Now, type the following code in the Source Editor. Be sure to outdent before you start typing.

        def readFeed(self):
    ok = False
    #####################################
    # If url passed in is blank, then use a default
    #####################################
    if self.inUrl != '':
    rssUrl = self.inUrl
    else:
    rssUrl = "http://www.dzone.com/feed/frontpage/java/rss.xml"
    #####################################
    # Parse feed located at given URL
    #####################################
    try:
    feedUrl = URL(rssUrl)
    input = SyndFeedInput()
    feed = input.build(XmlReader(feedUrl))
    ####################################
    # Do something here with feed data
    ####################################
    print(feed)
    ok = True
    except Exception, e:
    print 'An exception has occurred', e
    if ok != True:
    print 'An error has occurred in this reader'

    NetBeans IDE also features code completion, as seen in the following image. The IDE makes suggestions for you as you type. Click Enter to accept the suggested completion.

    Code completion
  6. In the Source Editor, add the following code to your application. Be sure to outdent before you start typing the code.

    if __name__== "__main__":
    reader = FeedReader('')
    reader.readFeed()
    print '********Command Complete...RSS has been parsed********'
  7. Right-click in the Source Editor and choose Format

    The Source Editor should now resemble the following image.

    Source Editor with complete code
  8. Click the yellow light bulb next to the java.io import InputStreamReader import statement and choose Remove All Unused Imports, as seen in the following image.

    Removing unused imports
  9. In the Source Editor, place your cursor over the yellow light bulb that remains in the left margin. The IDE displays a tool tip warning you that you have used an invalid function name, as seen in the following image. This is only a warning and can be ignored in this application.

    Warning of invalid name

Adding Required Libraries to the Project

Before you can run your application, you must add the required libraries to the Python/Jython path for this particular application you need a copy of ROME and JDOM jar files.

  1. Download the ROME and JDOM jar files to your computer. Be sure to note the their location.
  2. In the Project window, right-click the project node and choose Properties, as seen in the following image.

    Choose Properties in Project window

    The Project Properties window opens.

  3. In the Project Properties window, select Python in the Categories pane.
  4. Click Add and browse to the location of the ROME and JDOM jar files. Click Choose to add them to the Python path.

    The jar files are displayed in the Python Path pane of the Project Properties window, as seen in the following image.

    Project Properties window

Running the Application

Now it's time to test the application, In NetBeans IDE, application out is displayed in the Output window.

  1. In the Projects window, right-click the project node and choose Run, as seen in the following image.

    Running the application
  2. View the results in the FeedReader tab of the Output window, as seen in the following image.

    FeedReader tab in Output window

Summary

In this tutorial, you created a simple Jython application using NetBeans IDE, added user input, and then added input validation. You also experimented with code refactoring and code completion, and viewed the application's output in the Output window of the IDE.

See Also


This page was last modified: December 10, 2009