DevelopingJythonAppsUsingNetbeans

Developing Jython Apps Using Netbeans

Introduction


Since Netbeans 6.5 has been released, Python and Jython support has been available via the nbPython plugin. There is quite a bit of documentation available for this plugin in various locations. In this article, I will tie together some of the different references for this plugin and I will show you how to generate a small Jython application which uses some Java libraries contained within an external JAR file.


Setup


The nbPython plugin is easy to install and configure. In most cases, it is as simple as installing the plugin via the plugin manager and then restarting the IDE. If the auto-configuration detects your various environments then you will be ready to go. Configuration for the Netbeans Python support is performed within the "Python Platform Manager" window. This window allows you to add Jython or Python installations so that Netbeans can use them, you can also remove them here. Another nice feature of the platform manager window is that it allows you to set up 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


If your Jython or Python installation does not appear by default within the platform manager window, it is easy to add. Simply click on the "New" button below the listing of currently configured installations and an "Open" dialog box will appear which allows you to select the Jython or Python executable you wish to define. Once you have selected the Jython or Python installation of your choice then hit "Open" and it should be added to your configuration.

Steps for Installation and Configuration


1. Install via the plugin manager and restart the IDE when prompted

2. When the IDE opens, configure your Python/Jython installations using the "Python Platform Manager".

File:nbwiki1_2_DevelopingJythonAppsUsingNetbeans.jpg

3. Ensure that default installations are in place. If they are not, then add them and set the default.

File:nbwiki2_1_DevelopingJythonAppsUsingNetbeans.jpg

Developing a Small Jython Application


We will develop a short RSS reader which was originally described within the Jython Monthly newsletter (provide link). Throughout the development, I suggest that you try to use some of the great features that are included with the nbPython plugin in order to make your development easier.

Let's start out by creating your Jython project. In order to do so, perform the following steps:

1) Choose File->New Project and select Python Project from within the New Project window then click next.

2) Let's name our project FeedReader and then keep the other default settings selected. (Please note that you may wish to deselect the "Set as main project" checkbox). Once you've done this then choose "Finish" to create the project.

File:nbwiki3_1_DevelopingJythonAppsUsingNetbeans.jpg

After you've completed the steps above, a new editor will open with a Python module named after your project "FeedReader.py". As you can see, Netbeans automatically documents the author and date of the project along with providing a short sample print "Hello" program. We will of course remove the short sample program and replace it with our RSS feed reader utility.

Now let's begin coding the RSS Reader by typing the import statements as you see them below:

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

You will notice that Netbeans places a small yellow icon next to each import statement. This tells us that the particular import statement is not yet used in the application. These will go away as we begin to reference the imported classes.


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

Now let's continue on and type the code starting with the FeedReader class declaration. A few things to notice about the Netbeans editor:

1) Code is color sensitive, each keyword is given a blue color by default, and variables have an orange tone.

2) Netbeans assists you by adding keywords (such as "self"), parentheses, and colons where necessary, helping to shorten your required typing.

3) Indentation is taken care of by the IDE in most cases. You will be required to out-dent as needed in order to end a code block.

Okay, now that we've seen some of the basic editor features you can finish typing the rest of the small program into the editor. The complete program listing is below:

FeedReader.py


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


class FeedReader(Object):
    def '''init'''(self, url):
        self.inUrl = url
        
    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'

if '''name'''== "'''main'''":
    reader = FeedReader('')
    reader.readFeed()
    print '*******************Command Complete...RSS has been parsed***********************'   

Adding Required Libraries to Project

We will not be able to run the application successfully until we've included the required libraries (JAR files) in the Python/Jython path. For this particular application, you will need to obtain a copy of the ROME and JDOM jar files. See below for the resources.

Once you've obtained the necessary JAR files, you can add them by right-clicking on the project and selecting "Properties". Once the Project Properties window opens, you will want to select the Python button on the left-hand side of the window. Now, choose the "Add" button on the right-hand side and find/select the JAR files on your hard drive. That's it, when you run the program it will automatically include the libraries.

File:nbwiki4_1_DevelopingJythonAppsUsingNetbeans.jpg

Run the Program

Simply right-click on the project and select Run to see your program in action. Please note that this application runs fine with Jython 2.5 b0, but it did not run successfully with 2.5b1...I will look into that issue. You should see the feed being parsed in the Netbeans "output" window.


File:nbwiki6_1_DevelopingJythonAppsUsingNetbeans.jpg



Resources

Rome and JDOM Jar Files: rome-0_DevelopingJythonAppsUsingNetbeans.9.jar jdom_DevelopingJythonAppsUsingNetbeans.jar

nbPython Roadmap

Feature List

Video Demo


Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo