Fixing Jython "*sys-package-mgr*: can't write cache file for and processing new jar" messages in Linux and Solaris

Saturday, February 28, 2009

I know this is a nuisance for jython users, especially in Linux/Solaris environments if jython is installed as root in a common location where other users can invoke jython but can't cache jar files in their CLASSPATH in jython cachedir (in <JYTHON_HOME>/cachedir/packages) because of default permission problems for the jython cachedir directory, in fact the reason why I wanted to avoid these jar processing and error messages is because I use jython for automated testing and half of my test logs are filled with one or the other jar processing/error messages shown above, making it hard for debugging.

The obvious question is how can we avoid jython jar processing or error messages each time we invoke jython? Initially I didn't find a clear cut answer for this, obviously we need to set permissions accordingly, but which directory and how or is there a way we can change the jython registry settings to avoid this error, which is the best way, questions galore.

We will see two approaches as shown below to solve the jython jar processing messages/errors, one through setting the right permissions for jython cachedir directory and the other through modifying the jython registry settings, I have tested these in Solaris and Linux and of course the registry solution will also work for Windows.

1. Solving jython jar processing messages and errors by changing jython cachedir permissions
2. Solving jython jar processing messages and errors by changing jython registry settings

1. Solving jython jar processing messages and errors by changing jython cachedir permissions

As trivial it may sound but its not, for example assuming that the UNIX root user installed jython in the location /opt/jython and another UNIX user (jyuser in this example with home directory /export/home/jyuser) is trying to invoke jython with his own jars in the CLASSPATH (for this example I have included the JAR CLASSPATH of JDOM XML Parsing library as shown below), the error message which will be seen is

bash-3.00# su - jyuser
Sun Microsystems Inc. SunOS 5.10 Generic January 2005
-bash-3.00$ pwd
/export/home/jyuser
-bash-3.00$ echo $CLASSPATH
:/export/home/jyuser/jdom-1.1/lib/add_jdom_jars:/export/home/jyuser/jdom-1.1/lib/ant.jar:/export/home/jyuser/jdom-1.1/lib/jaxen-core.jar:/export/home/jyuser/jdom-1.1/lib/jaxen-jdom.jar:
/export/home/jyuser/jdom-1.1/lib/saxpath.jar:/export/home/jyuser/jdom-1.1/lib/xalan.jar:/export/home/jyuser/jdom-1.1/lib/xerces.jar:/export/home/jyuser/jdom-1.1/lib/xml-apis.jar:
-bash-3.00$ /opt/jython/jython
*sys-package-mgr*: processing new jar, '/export/home/jyuser/jdom-1.1/lib/ant.jar'
*sys-package-mgr*: can't write cache file for '/export/home/jyuser/jdom-1.1/lib/ant.jar'
*sys-package-mgr*: processing new jar, '/export/home/jyuser/jdom-1.1/lib/jaxen-core.jar'
*sys-package-mgr*: can't write cache file for '/export/home/jyuser/jdom-1.1/lib/jaxen-core.jar'
*sys-package-mgr*: processing new jar, '/export/home/jyuser/jdom-1.1/lib/jaxen-jdom.jar'
*sys-package-mgr*: can't write cache file for '/export/home/jyuser/jdom-1.1/lib/jaxen-jdom.jar'
*sys-package-mgr*: processing new jar, '/export/home/jyuser/jdom-1.1/lib/saxpath.jar'
*sys-package-mgr*: can't write cache file for '/export/home/jyuser/jdom-1.1/lib/saxpath.jar'
*sys-package-mgr*: processing new jar, '/export/home/jyuser/jdom-1.1/lib/xalan.jar'
*sys-package-mgr*: can't write cache file for '/export/home/jyuser/jdom-1.1/lib/xalan.jar'
*sys-package-mgr*: processing new jar, '/export/home/jyuser/jdom-1.1/lib/xerces.jar'
*sys-package-mgr*: can't write cache file for '/export/home/jyuser/jdom-1.1/lib/xerces.jar'
*sys-package-mgr*: processing new jar, '/export/home/jyuser/jdom-1.1/lib/xml-apis.jar'
*sys-package-mgr*: can't write cache file for '/export/home/jyuser/jdom-1.1/lib/xml-apis.jar'
*sys-package-mgr*: can't write index file
Jython 2.2.1 on java1.6.0_07
Type "copyright", "credits" or "license" for more information.
>>>

Remember jython caches jars in <JYTHON_HOME>/cachedir/packages (in our case /opt/jython/cachedir/packages), therefore I changed the permissions of /opt/jython/cachedir/packages (cachedir/packages) to 777 and tried the above command, the results are shown below.

-bash-3.00$ /opt/jython/jython
*sys-package-mgr*: processing new jar, '/export/home/jyuser/jdom-1.1/lib/ant.jar'
*sys-package-mgr*: processing new jar, '/export/home/jyuser/jdom-1.1/lib/jaxen-core.jar'
*sys-package-mgr*: processing new jar, '/export/home/jyuser/jdom-1.1/lib/jaxen-jdom.jar'
*sys-package-mgr*: processing new jar, '/export/home/jyuser/jdom-1.1/lib/saxpath.jar'
*sys-package-mgr*: processing new jar, '/export/home/jyuser/jdom-1.1/lib/xalan.jar'
*sys-package-mgr*: processing new jar, '/export/home/jyuser/jdom-1.1/lib/xerces.jar'
*sys-package-mgr*: processing new jar, '/export/home/jyuser/jdom-1.1/lib/xml-apis.jar'
*sys-package-mgr*: can't write index file
Jython 2.2.1 on java1.6.0_07
Type "copyright", "credits" or "license" for more information.
>>>

This solves the *sys-package-mgr*: can't write cache file error message, but still the *sys-package-mgr*: processing new jar message occurs everytime jython is invoked, after hacking around for sometime, finally the below chmod command did the trick for me.

chmod -R 777 <JYTHON_HOME>/cachedir/ chmod -R 777 (/opt/jython/cachedir/ in our case)

-bash-3.00$ /opt/jython/jython
Jython 2.2.1 on java1.6.0_07
Type "copyright", "credits" or "license" for more information.
>>>

Note that this will still process the jars the first time you invoke jython with thenew jars in the CLASSPATH, to avoid that execute <JYTHON_HOME>/jython > /dev/null 2>&1 and invoke jython again.

2. Solving jython jar processing messages and errors by changing jython registry settings

There is another way to overcome jython jar processing and error messages by modifying jython registry settings in <JYTHON_HOME>/registry properties file,

Change the line

#python.cachedir.skip = false to

python.cachedir.skip = true

In don't know the side effects of it as it was mentioned in the registry file above this property that it might break some java imports, if you don't want such unpleasant surprises, then go for the solution 1.

In a nutshell, to fix jython jar processing and error messages

1. Change the permissions which will allow all users to write to jython cachedir, chmod -R 777 /cachedir/
2. Set the the jython registry (<JYTHON_HOME>/registry) property python.cachedir.skip = true.

Aribas - A Large Integer Math Library and Interpreter

Tuesday, February 24, 2009

Large Integer Arithmetic tool is always a boon for cryptographers and other math researchers, when I was in my college days I had a passion for Math, especially Prime Factorization and Public Key Cryptography, somehow I was not comfortable with using high level languages like java or python for these tasks despite their support for large integer arithmetic, but I needed some specific math libraries, after many days of search, I found a complete standalone large integer math library - Aribas.

Aribas is a large integer math interpreter written by Prof. Dr. Otto Forste with a rich collection of math libraries which can be used for cryptography and other number theory research, it can be called as a programming language in itself where you can write specific scripts and execute them using Aribas, something which will be very handy for programmers who want to develop their own utilities for specialized math functions and also integrate it to high level languages like java or C for robust math support.

Even though it has an excellent documentation, I would like to highlight some cool features of Aribas here so that it will make a best impression to you, so the next time you need to work with cryptography or any math project involving large integers, you can jump into this tool straightaway, the main motivation for me to write about this is I could find this highly effective tool only after a rigorous search for a large integer math library.

1. Downloading, Invoking the Aribas interpreter
2. Aribas Settings
3. Scripting in Aribas
4. Important Large Integer Math functions in Aribas
5. A small tip (write it in your memory)..

I am confident that once you skim through these sections, you will be bookmarking Aribas, thanks Prof. Dr. Otto Forste for this great tool.

1. Downloading, Invoking the Aribas interpreter

Aribas is a lightweight tool which you can download from here, its available on all flavors of UNIX and Windows.

To invoke the interactive interpreter (something similar to the one in languages like python), unzip Aribas archive (aribasw.zip) and run aribasw executable from aribas directory, you are all set you explore the math world in seconds!

Fig 1: Invoking the Aribas Interactive Interpreter

2. Aribas Settings

A couple of important Aribas settings which comes to my mind are

2.1. Floating Point Precision Settings

This is something you can configure through the interactive interpreter GUI (Aribasw -> Preferences -> FloatPrec) or in command line using the set_floatprec() function, depending on the precision, your floating point results will vary, for example consider the value of pi (22/7), the difference in results with floating point precisions set to 32 and 128 are shown below.

==> set_floatprec(32).
-: 32

==> 22/7.
-: 3.14285714

==> set_floatprec(128).
-: 128

==> 22/7.
-: 3.14285_71428_57142_85714_28571_42857_14285_7

==>

2.2 Memory Settings

Since large integer math is a memory intensive operation and also Aribas is a language in itself, it uses its own heap and you can specify the memory size (in KB) which Aribas can use to execute your code (between 512KB to 32MB), something similar to specifying the heap size for JVM, for example you can specify a heap size of 10MB for Aribas by invoking Aribas with the -m option shown below

aribasw -m10000 (Uses upto 10MB)

Another useful function is memavail() which you can invoke to get the reserved/free memory available, for the above invocation of Aribas (with heap size of 10MB), the results I got were shown below.

==> memavail().

total number of garbage collections: 0
5091840 Bytes reserved; 5091840 Bytes active (44 used, 5091796 free)
58079 Bytes free for user defined symbols and symbol names
-: 4972

==>

3. Scripting in Aribas

Aribas is a complete language in itself for its purpose of large integer math support, this means it has control structures like if-else, looping constructs like for, while, file handling routines, I/O, different data types like Arrays, Strings, etc, in short you can have your own Aribas project with multiple source files for developing a tool which may solve your problem.

Look at some sample Aribas scripts here to understand the coding part of it.

4. Important Large Integer Math functions in Aribas

4.1 Computing multiplicative inverse

This is an important function used frequently in cryptography and other math algorithms, the multiplicative inverse, multiplicative inverse of a and b is an integer m where (a * m) % b = 1

The multiplicative inverse of 5 and 24 is 5.

==> mod_inverse(5, 24).
-: 5

==>

The multiplicative inverse of 6 and 31 is 26.

==> mod_inverse(6, 31).
-: 26

==>

4.2 Factorization and Prime numbers

There are several functions available for factoring a number using different algorithms which will be handy for researchers, like the quadratic sieve factorization algorithm shown below.

==> qs_factorize(437). (437 = 23 * 19)
-: 19

==>

Another function I found very useful is finding the next prime number

==> next_prime(35).
-: 37

==>

And primality tests

==> prime32test(119). (17 * 7)
-: 0 (false)

==> prime32test(179).
-: 1 (true)

==>

Believe me, these functions will reduce your effort greatly when you dealing with very large numbers.

5. A small tip (write it in your memory)..

For users of other scripting languages like python, this is something which needs to be bootstrapped in mind when you program in Aribas, end all your statements with a dot (.), please don't ask me how many times I keyed in Enter before doing that :).

Fixing Internet Explorer (IE) Sidebar Float Drop Problem in Blogger

Monday, February 23, 2009

I didn't realize the IE Sidebar float drop problem for my blogger template for months, after which it came to notice accidentally when I was doing a browser compatibility check for posts on my blog. There is no right sidebar in IE (yes it dropped down), although it had enough space on the right for that sidebar to float, also I could see the right sidebar in Firefox and Chrome, then realized that its a problem specific to IE, the sidebar float drops and I made a minor code change in my template to fix it, which we will see below.

As a first practice, I was checking the solution with Google for a quick fix, but my blog is not a fixed width one, therefore some solutions did't prove fruitful to me, then I decided to explore the possible ways the left and right sidebars can float and one of that combination clicked.

For this example, we will a sample three column template from here, since in my case the sidebars and the post section are not fixed width, my template code for sidebar, main-wrapper and content-wrapper looks like the below one (this blog used this template code before the IE sidebar float drop fix).

Listing 1: Blogger code snippet for my post and sidebars (before fixing the IE Sidebar float drop problem)
#content-wrapper {
width: 98%;
margin: 0 1%;
padding: 0;
text-align: left;
background-color: $mainBgColor;
border: 1px;
border-top: 0;
}

#main-wrapper {
width: 66%;
float: left;
margin: 0 1%;
background-color: $mainBgColor;
word-wrap: break-word; /* fix for long text breaking sidebar float in IE */
overflow: hidden; /* fix for long non-text content breaking IE sidebar float */
}

.sidebar-wrapper {
width: 16%;
float: left;
margin:0 auto 0;
background-color: $mainBgColor;
word-wrap: break-word; /* fix for long text breaking sidebar float in IE */
overflow: hidden; /* fix for long non-text content breaking IE sidebar float */
}
For your understanding in the above CSS declaration,

#content-wrapper - wraps the post and the sidebar sections
#main-wrapper - wraps the post section
#sidebar-wrapper - wraps the sidebar (left and right sidebars)

As you see from the above CSS, the main-wrapper, which wraps the post has a margin of 1%.

Fig 1: Blogger Post on Firefox and IE (before fixing the IE Sidebar float drop problem)



To fix the IE sidebar drop problem in the above code, I decided to have two sidebars, one for the left and right and tweak their code separately, therefore I used two sidebars, namely the left-sidebar-wrapper and the right-sidebar-wrapper with similar code as a first step to address this issue, after doing this all you need to do is to replace the first occurrence of sidebar-wrapper (after its declaration) in the template code to left-sidebar-wrapper and the second occurrence to right-sidebar-wrapper, then remove the sidebar-wrapper declaration as we no longer need that.

As a next step, I decided to change the float property on my right-sidebar to float:right, remove the margin property for my main-wrapper (which wraps the post) and have a right margin of 1% for my left-sidebar-wrapper (with float:left) and left margin of 1% for my right-sidebar-wrapper (float:right) and it WORKED!!!

The code snippet which addresses the IE Sidebar float drop problem for my blog now is

Listing 2: Blogger code snippet for my post and sidebars (after fixing the IE Sidebar float drop problem)
#content-wrapper {
width: 98%;
margin: 0 1%;
padding: 0;
text-align: left;
background-color: $mainBgColor;
border: 1px;
border-top: 0;
}

#main-wrapper {
width: 66%;
float: left;
background-color: $mainBgColor;
word-wrap: break-word; /* fix for long text breaking sidebar float in IE */
overflow: hidden; /* fix for long non-text content breaking IE sidebar float */
}

.left-sidebar-wrapper {
width: 16%;
float: left;
margin: 0 1% 0 0;
background-color: $mainBgColor;
word-wrap: break-word; /* fix for long text breaking sidebar float in IE */
overflow: hidden; /* fix for long non-text content breaking IE sidebar float */
}

.right-sidebar-wrapper {
width: 16%;
float: right;
margin: 0 0 1% 0;
background-color: $mainBgColor;
word-wrap: break-word; /* fix for long text breaking sidebar float in IE */
overflow: hidden; /* fix for long non-text content breaking IE sidebar float */
}
Fig 2: Blogger Post on Firefox and IE (before fixing the IE Sidebar float drop problem)



Summary...

To fix the IE Sidebar float drop problem for your blog its recommended to

1. Use two sidebars, left-sidebar-wrapper and right-sidebar-wrapper with the left-sidebar-wrapper having float:left and the right-sidebar-wrapper having float-right property.

2. Its better to use a right margin in left-sidebar-wrapper and a left margin for the right-sidebar-wrapper than having a margin for the main-wrapper for posts fix the IE sidebar drop problem as shown above.

Netbeans Python IDE features: Compiling and Running Jython Projects and Shortcuts

Tuesday, February 17, 2009

In my previous post, we looked at Netbeans IDE for CPython, Configuring and Running CPython Projects and IDE shortcuts, for the sake of completeness, in this article, we will look into Configuring and Running Jython (JPython) projects from Netbeans, though both are similar languages with different implementations, I wanted to explore how jython features would work in Netbeans, especially the IDE features and configuration for java class libraries accessible from jython.

For those of you who want to follow this article series from the beginning, I recommend you to look at my introduction on Netbeans IDE for Python and Jython, the basic introduction would help you to create a Python project, configure platform settings for projects, etc.

Netbeans Python IDE would be the first integrated IDE for CPython as well as JPython and we will see the following JPython IDE features in this section.

1. Jython Platform settings and running the interactive jython console
2. Jython Library Settings
3. Autocompletion features for jython modules, built in functions and java class libraries
4. Running Jython projects and modules, configuring command line options
5. Important IDE Shortcuts

1. Jython Platform settings and running the interactive jython console

Now you have an active Jython project, the first thing which you must always do is to change the platform settings to jython for the interactive python console as well as for compiling/running the jython project (if you had created a new jython project, you would have configured the compilation/run time platform to jython already).

The good thing is Jython 2.5 is bundled with Netbeans IDE, so it is the default platform for Interactive Python Interpreter, in any case from Netbeans IDE, click Tools -> Python Platforms -> Select Jython (2.5 in this example), and click "Make Default" button if its not the default platform configured already, see the figure below.

Fig 1: Jython Platform Settings

Also you can change the project property settings to change the platform to jython as shown in the figure below (Project Properties -> Python -> Python Platform).

Fig 2: Changing Python Platform settings through Project Properties

From the Netbeans python IDE, you can run the interactive jython console (this is available on all jython installations, the one you will get when you invoke jython without arguments), to start jython interactive console, all you need to do is to invoke Window -> PythonConsole as shown in the figure below, make sure that the platform shown is java when the console starts, you should then be able to import jython/java class libraries and access their methods.

Fig 3: Jython Interactive Console

2. Jython Library Settings

Jython has core/third party python libraries which run on JVM as well as java class libraries, therefore if you need to add your own python libraries to the project, you can add them through Tools -> Python Platforms -> Python Path -> Add -> Library Path and java libraries through Tools -> Python Platforms -> Java Path -> Add -> Library Path (or jar file locations).


Fig 4: Jython Library Path Settings

3. AutoCompletion features for jython modules, built in functions and java class libraries

Though this is similar to the one we explored for CPython libraries and built in functions, the autocompletion feature doesn't work as I expected for java class libraries, but I am sure that if standard python built in functions like dir() works for java class libraries, this feature may also work in future versions (will update it by then).

Netbeans Python IDE features: Configuring and Running Python Projects and Shortcuts

Sunday, February 15, 2009

In my previous post, we looked at the Netbeans IDE for python and jython, creating a new python project, settings and a high level overview of the IDE, in this article we will see in detail , the Netbeans CPython IDE features, the main reason why I am writing this is because I always wanted to have a quick reference on using the IDE features which I use extensively, sometimes its not easy to recollect IDE shortcuts if you are using many IDEs for projects involving different technologies, as is the case with me when I use the JCreator IDE for small java projects, most of the times I hit the walls on figuring out some project settings, command line options and other IDE configurations.

This writing may serve as a useful guide to Netbeans Python IDE users on some important shortcuts and project settings for CPython, for Netbeans users, this may be an easy transition, but for python users using IDLE for their projects, there are small changes in the way the similar shortcuts work in Netbeans.

Lets go to individual sections, in some sections I will be highlighting the differences between Netbeans Python IDE and IDLE features.

1. Python platform settings and running the interactive python console
2. Python Library Settings
3. Auto Completion feature for library and built in functions
4. Running Python projects and modules, configuring command line options
5. Important Python IDE Shortcuts

I assume that you know the basics features of Netbeans Python IDE, if not I recommend you to go through my introductory article on Netbeans Python IDE for Python and Jython.

1. Python platform settings and running the interactive python console

Now you have an active CPython project, the first thing you must always do is to change the Python Platform settings to CPython as shown in the figure below, note that right now this setting is not retained after you exit the IDE, but this may change in the later releases.

In Netbeans IDE, click Tools -> Python Platforms -> Select Python (2.5 in this example), and click "Make Default" button, note that Jython is the default platform used in Netbeans as its bundled with the Netbeans Python IDE.

Fig 1: Python Platform Settings

From the Netbeans python IDE, you can run the interactive python console (this is available on all python installations with the IDLE Python console), to invoke the python interactive console, all you need to do is to invoke Window -> PythonConsole as shown in the figure below.

Fig 2: Python Interactive Console

2. Python Library Settings

Python has its core libraries and third party libraries available through other python extension projects (lie PythonWin, SciPy, etc), all third party libraries are installed in /Lib/site-packages, you can view the standard python library search paths included by default (Tools -> Python Platforms -> Python Path) as shown below, you can also add your own library paths here where python can search for a module (Add -> Library Path).

Fig 3: Python Library Path Settings

3. Auto Completion feature for library and built in functions

Autocompletion is an important feature of any IDE, as obvious its supported for all libraries included in python search path as shown below, however a minor difference I noted is that it doesn't highlight this for built in python functions like type, str, etc (which IDLE does).

Fig 4: Python Auto Completion feature in Netbeans

4. Running Python projects and modules, configuring command line options

You can run a Python project (Main project source file) or an individual module using Run -> Run Project or Run File, but right now for individual python modules, this option is not included as a right click property, which Netbeans supports for java, though it should be available in future versions.

Fig 5: Running Python projects and modules

Command line options can be specified under Project Properties -> Run -> Application Arguments as shown below.

Fig 6: Specifying command line arguments for projects

5. Important Python IDE Shortcuts

Hope now you are excited about Netbeans python IDE and its features, if you use shortcuts frequently as I do for IDLE, then the following frequently used shortcuts will be handy.

1. Running a Python project - F6 (F5 in IDLE)
2. Running an individual python module - Shift + F6
3. Comment out Code Block - Ctrl + Shift + C
4. Indent Code Block - Alt + Shift + Right
5. Dedent Code Block - Alt + Shift + Left
6. Code Completion - Ctrl + Space

In the next post, we will see specific options and features available for JPython interpreter.

Netbeans IDE support for python and jython and its usage

Saturday, February 14, 2009

With dynamic languages getting widespread acceptance and is being used in numerous applications, especially languages like CPython and JPython becoming popular among the developer community, Netbeans has added support for CPython and JPython in its IDE, the initial support is available in Netbeans 6.5, from my experiences with it, it looks very promising and is poised to become the best open source IDE for python and jython languages in near future.

Netbeans 6.5 supports all standard IDE features for python, if you are a java programmer using Netbeans IDE, this would be a welcome development, I know many Netbeans folks interested in python/jython asking me for a similar IDE available for these languages, if you are a core java developer, I agree that its not easy to come out of the Netbeans IDE style of work and thus the support for these languages is always imminent.

This tutorial is designed to quickstart you to Netbeans IDE with python and jython, its has three sections.


1. Netbeans IDE features, Installation, Creating a Python Project and Settings
2. Configuring and running Python projects from Netbeans IDE and Shortcuts
3. Configuring and running Jython projects from Netbeans IDE and Shortcuts

1. Netbeans IDE features, Installation, Creating a Python Project and Settings

1.1 Download: Netbeans 6.5 for python early access version here.

1.2 Opening a new python/jython project

Prerequisites:

Python: Coincidentally at the time of this writing, Python 3.0 was released, but for some reasons, the Netbeans IDE early access doesn't detect python 3.0, but in future it should support Python 3.0 and I will update it in this post when it supports python 3.0.

Jython: The Netbeans IDE 6.5 comes with a default jython package installed (Jython 2.5 at the time of this writing), but if there are additional versions installed in your system, you can choose the one which will be used for that project.

Once you have python/jython installed, you can open a new project as shown in the figure below.

Step 1: Open File -> New Project


Fig 1: Opening a new python project

Step 2: Selecting or adding a new python platform


Fig 2: Default Python platform available

As shown in the above figure, by default, the jython 2.5 platform which is bundled with Netbeans will be shown, but you can add your own python platform

Step 3: Adding your own python platform

Even though this will not work for Python 3.0 currently, any 2.x python versions can be added as shown below, you can skip this part if you have an existing python 2.x installation detected by Netbeans.

Click Manage tab next to Python Platform selection option, select the path to python 2.5 executable, you can then see the new python platform added.


Fig 3: Adding your own python platform

After you add a python 2.x platform, you can set it as the default platform for your project if you want to compile it using CPython, see Fig 4 below.


Fig 4: Selecting the python platform for the project

Once you select the python version, click finish and you are all set you start your python journey with Netbeans as shown below.


Fig 5: Python Project IDE

2. Configuring and running Python projects from Netbeans IDE and Shortcuts

3. Configuring and running Jython projects from Netbeans IDE and Shortcuts

Jython FAQ: Differences between Jython and Java

In my previous posts, we have seen some important concepts in java and jython languages, in this FAQ section we will see the major differences between java and its scripting counterpart, namely jython. Even though seasoned java/python programmers can easily figure out the benefits of python implementation in JVM, but for those who are wondering what jython is and whats in it for java/python programmers or if you ask simply why should I care about jython, please follow this article.

For a customary introduction, the idea implementing of open source scripting languages in java started some years back and has given birth to many interesting languages like JRuby, Groovy, Tcl/Java, Jelly, BeanShell, Jython, Rhino, etc. In fact there is an interesting article on choosing a scripting language for java, in this section we will discuss things specific to jython.

1. What are the common applications of jython?

Jython is used mainly areas like unit testing, servlet programming, GUI programming and other lightweight applications on JVM, in general if you want a tool to simplify the way you want to work with java class libraries, then jython is for you.

2. What are the IDEs available for jython?

The best known IDE is the Netbeans port for dynamic languages, coyote, which includes jython as well, so if you are using netbeans for java, you can use coyote for jython application development.

If you are a C python programmer, IDLE will also help to make sure that you don't have any syntax errors and develop code with proper indentation rules for python/jython, then you can run the jython code in the command line using the jython interpreter (the method I prefer, I am a CPython programmer too), IDLE comes with CPython distribution.

With these basics, we can now see some differences between jython and java.

3. What about inheritance in jython from java?

Jython follows java rules, which means there is no support for multiple inheritance as you would do in CPython, so if feel you are smart like me to override java rules from jython, it will be in vain as I experimented before, do look at my article on extending java classes from jython to learn more. The important points are

3.1 Multiple inheritance is not allowed in jython, if you try to do so, you will get an error TypeError: no multiple inheritance for Java classes.

3.2 As obvious, when you have two java classes with the same name with one implemented in java and the equivalent one in jython, any jython code which imports the java class will import the equivalent jython class module.

4. How about implementing java interfaces in jython?

Implementing a java interface in jython is something similar to extending a java class which has no methods (the java interface), the jython class need not define all the java interface methods, but if it tries to invoke a method from the java class which is not overridden, an exception AttributeError: abstract method "javamethodname" not implemented, for more details do look at my detailed article on implementing java interfaces in jython.

In addition to these there are many subtle differences between java and jython like the way you can get the java class/interface objects and names in jython, the way jython handles java data types, etc, we will be looking at those in my future posts related to jython and java or continued from here.

Getting the Class Object and name of a Java Class and Interface in Jython

Monday, February 9, 2009

In my last post, we have seen how to get the Class Object and name of a java class and interface from java, in this post, we will see how to get the Class Object and name of a java class and interface from jython, for those of you who are new to jython, its an implementation of Python programming language on JVM, from jython, in addition to standard python libraries, you can also access java class libraries, create instances and invoke their methods.

I have been blogging about jython for quite some time and its become my habit to explore the equivalent jython script of any java code I write and that infact has thrown some surprises, its not a straightforward conversion at times and there are subtle differences in the way jython and java works, but if you are a java programmer who want to explore jython, there is no need to panic as jython follows java rules, so whatever java concepts you have learned is applicable for jython (to comply to java in itself is a great design challenge for jython developers).

Lets come back to the topic on how to get the Class Object and name of a java class and interface from jython, see the below code and its explanation.

Listing 1: ClassName.py
1.  ##
2. ##ClassName.py
3. ##Author: S.Prasanna
4. ##
5.
6. from javax.swing.text.html import HTML
7. from java.lang.management import RuntimeMXBean
8. from java.lang import Class
9.
10. ##Getting the Class Object and name of a java class
11.
12. htmlObj = HTML()
13. print "HTML Class Object using getClass method =", htmlObj.getClass()
14. print "HTML Class Object using Class.forName =", Class.forName("javax.swing.text.html.HTML")
15. print "HTML Class Object name using getName method=", htmlObj.getClass().getName()
16.
17. ##Getting the Class Object and name of a java interface
18.
19. print """RuntimeMXBean Class Object using RuntimeMXBean.class =""", RuntimeMXBean.class
20. ##Returns java.lang.Class, not the right Class object of RuntimeMXBean, it
21. ##should be java.lang.management.RuntimeMXBean, whereas RuntimeMXBean.class
22. ##in java would return java.lang.management.RuntimeMXBean
23.
24. print "RuntimeMXBean Class Object using Class.forName =", Class.forName("java.lang.management.RuntimeMXBean")
25. print "RuntimeMXBean Class Object name using getName method =" , Class.getName(Class.forName("java.lang.management.RuntimeMXBean"))
26. ##This would return java.lang.management.RuntimeMXBean since
27. ##java.lang.management.RuntimeMXBean is a valid interface, therefore in
28. ##jython to get a Class object of an interface, use Class.forName and the
29. ##full qualified package and interface name as an argument
30.
Output:

C:\jython>jython ClassName.py
HTML Class Object using getClass method = javax.swing.text.html.HTML
HTML Class Object using Class.forName = javax.swing.text.html.HTML
HTML Class Object name using getName method= javax.swing.text.html.HTML
RuntimeMXBean Class Object using RuntimeMXBean.class = java.lang.Class
RuntimeMXBean Class Object using Class.forName = java.lang.management.RuntimeMXBean
RuntimeMXBean Class Object name using getName method = java.lang.management.RuntimeMXBean

Explanation:

We are importing a java class (line 6, javax.swing.text.html.HTML) and an interface (line 7, java.lang.management.RuntimeMXBean) for which we are going to get the Class Object and its name. For regular java classes, getting the Class Object and its name works similar in java as well as jython, either you can use getClass method (line 13) or Class.forName method with the fully qualified package and class name as argument (line 14) and the getName method of the Class object returns the name of the class (line 15).

For java interfaces, the output is little different in jython compared to java, in java you can use the .class attribute of the interface to get the java interface's Class Object, in jython even though you can use the .class attribute for a java interface, but it doesn't return the exact Class object name of that java interface (line 19, where RuntimeMXBean.class returns java.lang.Class and not java.lang.management.RuntimeMXBean, but RuntimeMXBean.class in java would return "interface java.lang.management.RuntimeMXBean", see here).

Therefore to get the Class Object of a java interface using jython, you should only use Class.forName method with the fully qualified package and interface name as an argument (line 24) as the .class attribute of the java interface may not work correctly in jython (also note that the .class attribute can only be used in jython and its not valid in CPython, but class is a keyword in both CPython as well as JPython), then to get the Class Object name of a java interface in jython, you can invoke the getName method of the Class object you got through Class.forName (line 25, its redundant though as Class.getName(Class.forName("java.lang.management.RuntimeMXBean")) would return "java.lang.management.RuntimeMXBean" ). The moral, there is always a difference between jython and java.

Getting the Class Object and name of a Java Class and Interface

Sunday, February 8, 2009

I know the title might be little confusing, but its very simple, the question is how can I get the name of a java class or an interface in a String format? Here we will explore the different methods we can use to get the Class Object and name of a regular java class and a java interface.

Every regular java class has an inherited getClass method which returns the run time Class object of that java class, for java interfaces, the Class object is got by using the .class attribute of that interface, we can also use the forName method of a Class to get the Class Object and name of a regular java class or an interface, once you get the Class Object of a java class or an interface, you can use the getName method of that Class to get the name of the java Class Object in a String format, still confused, go through the below code on how to do the same in java for regular classes and interfaces and the differences between them.

Another question which may arise is why should anyone bother to get the Class object or name of a java class or an interface and where its used? There are many occasions where you need to pass the runtime Class object of a java class or an interface to a method, for example in MBean applications there are lots of methods which need the Class object of an MBean (interface) to be passed to get the corresponding MBean instance on which you can invoke its methods (Object Reflection), if you have never come across MBean, take it easy, here we are just going to discuss the different ways to get the Class Object and name of a java class and interface, see the code below.

Listing 1: ClassName.java

1: /**
2: * ClassName.java
3: * Author: S.Prasanna
4: */
5:
6: import javax.swing.text.html.HTML;
7: import java.lang.management.RuntimeMXBean;
8:
9: //Getting the Class Object and name of a java class and interface
10: public class ClassName {
11:
12: public static void main (String args[]) {
13:
14: // Getting the Class Object and name of a java class HTML
15:
16: HTML htmlObj = new HTML();
17: System.out.println("HTML Class Object using getClass method = "
18: + htmlObj.getClass());
19:
20: try {
21: System.out.println("HTML Class Object using Class.forName method = "
22: + Class.forName("javax.swing.text.html.HTML"));
23: } catch (Exception e) {
24: e.printStackTrace();
25: }
26:
27: System.out.println("HTML Class Object name using getName method = "
28: + htmlObj.getClass().getName());
29:
30: // Getting the Class Object and name of a java interface RuntimeMXBean
31:
32: System.out.println("RuntimeMXBean Class Object using RuntimeMXBean.class = "
33: + RuntimeMXBean.class);
34: try {
35: System.out.println("RuntimeMXBean Class Object using Class.forName method = "
36: + Class.forName("java.lang.management.RuntimeMXBean"));
37: } catch (Exception e) {
38: e.printStackTrace();
39: }
40:
41: System.out.println("RuntimeMXBean Class Object name using getName method = "
42: + RuntimeMXBean.class.getName());
43: }
44: }

Output:

HTML Class Object using getClass method = class javax.swing.text.html.HTML
HTML Class Object using Class.forName method = class javax.swing.text.html.HTML
HTML Class Object name using getName method = javax.swing.text.html.HTML
RuntimeMXBean Class Object using RuntimeMXBean.class = interface java.lang.management.RuntimeMXBean
RuntimeMXBean Class Object using Class.forName method = interface java.lang.management.RuntimeMXBean
RuntimeMXBean Class Object name using getName method = java.lang.management.RuntimeMXBean

Explanation:

The above code is quite simple, we are importing a class (line 6, javax.swing.text.html.HTML) and an interface (line 7, java.lang.management.RuntimeMXBean) and for the HTML class, we are getting its Class Object using its inherited getClass method (lines 17 - 18) or through the Class.forName method (lines 21 - 22, here we have to pass the fully qualified package and class name to the forName method). Then we are printing the Class name as a String using the Class Object's getName method (lines 27 - 28).

For java interfaces, getting its Class Object and the class name is little different, since an interface is not a regular class, we can't invoke a method like getClass to get the corresponding class object, therefore we need to use the .class attribute of the interface to get the corresponding class object (line 32 - 33) or we can use the Class.forName method to get the Class Object by passing the fully qualified package and class name as its argument (line 35 - 36), to get the Class name of the interface, we can use the getName method on the Class Object we got using .class attribute of that interface (lines 41 - 42).

In my next post, we will see how to get the Class Object and the name of a java class and interface using jython, you are in for some small surprises as things are little different in jython and I don't want to confuse you about that in this writing, especially if you are a core java programmer.


Copyright © 2016 Prasanna Seshadri, www.prasannatech.net, All Rights Reserved.
No part of the content or this site may be reproduced without prior written permission of the author.