A programmer's tale

Tuesday, January 30, 2007

Java 1.5 Bug or What? Java 1.6 really rocks - A real life finding

Recently I have found a little but very interesting real life reason that why Java 6.0 is better, robust and stable JDK than Java 5.0; better to say any previous version.
I have written a dialog in Swing that accept user key for my current project REPro- a 100% pure Java reporting solution. We had migrated our project code base to Java 1.5 quite ago. That’s why the initial target Java platform for my Swing Dialog was Java1.5. Look at the sample code for that here:

public class LicenseDialog extends JDialog {

   public LicenseDialog(String title) {
      //..
      setTitle(title);
      setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
      setSize(400, 300);
      //..
   }
}

And the sample main class:

public class Main {
   public static void main(String[] args) {
      JDialog dlg = new LicenseDialog ("License Key");
      dlg.setVisible(true);
   }
}

Now our dialog is shown correctly but when I click the close button, it does nothing. The program doesn’t exit as expected. First time I’m very surprised, but that behavior meets our requirement, because it’s quite common to many License entry dialog; user can exit by clicking only “cancel” button. Hence I didn’t change my code, it remains unnoticed. But recently I tested our code from java 1.5 to Java 1.6. The problem arises. When I compiled and run the same code in Java 1.6, the program fails during run time giving the following error:

Exception in thread "main" java.lang.IllegalArgumentException:
defaultCloseOperation must be one of:
DO_NOTHING_ON_CLOSE,
HIDE_ON_CLOSE, or DISPOSE_ON_CLOSE

The cause: The method actually works correctly for only three parameter WindowConstants.DO_NOTHING_ON_CLOSE , WindowConstants.HIDE_ON_CLOSE or WindowConstants.DISPOSE_ON_CLOSE. If you put any other parameter, then it the method doesn’t tell you anything but doesn’t work correctly in Java 1.5. The method in Java 1.5 is :

public void setDefaultCloseOperation(int operation) {
   this.defaultCloseOperation = operation;
}

When you put JDialog.EXIT_ON_CLOSE in the method it doesn’t match the required parameter and nothing will happen. But dude, see the same method in Java 1.6:

public void setDefaultCloseOperation(int operation) {
   if (operation != DO_NOTHING_ON_CLOSE &&
    operation != HIDE_ON_CLOSE &&
    operation != DISPOSE_ON_CLOSE) {
    throw new IllegalArgumentException("defaultCloseOperation
    must be one of: DO_NOTHING_ON_CLOSE,
    HIDE_ON_CLOSE,
    or DISPOSE_ON_CLOSE");
   }
   int oldValue = this.defaultCloseOperation;
   this.defaultCloseOperation = operation;
   firePropertyChange("defaultCloseOperation", oldValue, operation);
}


It now handles the parameter correctly and gives the above run time error. It also change the "defaultCloseOperation" property of java.awt.Component by invoking firePropertyChange with required method. When I googled it I found the Bug entry for JDialog.

Java 1.6 is really the best JDK ever and it rocks. Some interesting facts about Java 1.6 are:
1. It is developed and tested on nearly 600 machines worldwide, most in a grid.
2. It covered three architectures: x86, AMD64, SPARC
3. 24 operating system including Red Hat linux 7.2, 9.0, RHAS 2.1, 3.0, 4.0, Fedora Core 3, 4, Solaris 8, 9, 10, 11, SuSE Linux 8.1, 0.9, 0.3, Windows 98 SE, ME, NT, 2000, 2000 Pro, 2000 Server, 2003, 2003 AS, XP Home, XP Pro and Vista, etc.
4. 84 tested configurations, 92,316 conformance tests (JCK)
5. 20,244 functional tests
6. 6,930 unit and regression tests
7. It’s test cycle length: 5 weeks – Tier 1, 10 weeks – All configurations.
8. For quality, compatibility and stability, Java 1.6 under gone continuous multi-platform testing.
9. It has community engagement including crack-the-verifier contest. There were 5 submissions but no successful attempts, regressions challenges; 133 submissions resulting in 72 bugs fixed.
You can see the other interesting things about Java 1.6 here.

Labels:

Saturday, January 27, 2007

How to learn effective Java? Part1: Core java

When I was in college, I used to do one particular thing every semester. I used to spend more time to collect the study materials and then study those to finish the courses. And I think it’s very common to most of the college students. But the prudent part was how wisely the time distribution is made. If you spend much more time to collect the materials, you cannot manage your time to study all the materials. On the other hand, if you don’t collect descent amount of study materials in your hand, you probably miss something in you courses.
In my professional life I also find some sort of similar thing. we need good Java materials and of adequate amount. We also have to manage our time to study them, do some hands-on lab on those to cope up with the fast moving Java technology advancement.
There are lots of tutorials on Java on the net. If you google "Java tutorials", you will find thousands of millions of Java tutorials. But most of those tutorials are not for professional and effective use.
Here are some of my personal findings and ideas about learning effective Java. This part is all about core Java.

1. Books:
Books are always the best friends if they are good enough. I suggest the following books for core Java concepts:
a) Java Programming Language, The, 4th Edition
It contains all core concept and all J2SE modules usage. This book is written by Java language designers and developers. Hence you will learn from masters.
b) JLS (Java Language Specification):
It contains all Java syntax, semantics and types. How the types can be used. Some examples here are much better than the previous one.
There are many popular books in the market for beginning Java courses. But many of these books are below standard and feed your brain with miss concepts. I haven’t read the book "Java And Object Oriented Programming Paradigm" by Debasish Jana. But I think it can be a good read. If anyone have read this book, please tell me about it.
Some other good core Java books are:
Core Java 2: Volume 1 and 2 by Cay S. Horstmann and Gary Cornell , Thinking in Java by Bruce Eckel and Java in a Nutshell by David Flanagan.
The best Java book I have ever read is "Effective Java Programming Language Guide" by Joshua Bloch. This book is very much useful for professional Java programmer. It has all the materials to write rock solid, stable and robust Java code. If you read the book thoroughly, I give you the guarantee that your debugging of Java code will be easy and take less time, you will learn how the experienced programmer can make a mistake, how hashCode of your class work, how to write efficient hashCode, how to serialize and deserialize objects correctly, what to write and what not to in Java and many many more things. Second volume of "Effective Java Programming Language Guide" will be published in this year end.
If you want to go deeper then you can read books on particular topics like concurrency, generics, Java io etc. I will tell those in the next sequel of this entry.

2. Online tutorial and Developer forum: Sun Java online tutorial is the best resources for online Java tutorials.
The other Sun Java resources contains Technical Articles, Technical Tips and also quizzes. The best way to get from it is to subscribe it’s SDN (Sun Developer Network) newsletter. You will get new articles and tips in your main box. The other developer networks are IBM developerworks, Oracle Java center. Some good sites are javalobby, javaworld and JDJ (Java Developer Journal). I also suggest to subscribe to their respective newsletter so that you will be updated with the latest happenings in Java world. You will also get the full digital addition of JDJ when you have the free subscription.

3. Browsing good Open Source project code:
Browsing Open Source code is one of my favorite ways for learning practical Java code. But be sure to browser the good quality open source code base. The Apache Foundation is the best place for open source code browsing. Have a look on these code bases if your time permits. Apache Foundation hosts many projects. All the projects may not be relivent for your learning. If you want to start from Core Java, then "commons" projects are the best place to start with. I also love other open source projects like JVolution, JScinece etc. You will know how far Java efficiency can reach in scientific computing and other sophisticated areas.

4. JavaOne and JavaPolis talk:
Last but not the least, frequently visit and watch the podcasts at JavaOne and JavaPolis.

These all are my personal learning paradigm for Core Java. If you have any suggestion, then please share your ideas. Hope I will discuss the effective learning of more advance Java like Swing, concurrency, Enterprise Java and patterns in Java here. Bye for now...

Sunday, January 21, 2007

Making with Maven2 machinery | Part2: making executable jar with multiple and nested dependencies

This is my second TIP for Maven2. This is about building jar and executable jar with multiple and nested dependencies in Maven2.
The plug-in needed for this purpose is maven assembly plug-in. Maven2 assembly plug-in home contains the definition and other information about assembly. It will be beneficial to have a look first.
I'm using three projects for this example.
1. swing-editor, 2. swing-ext and 3. java-ext.
1. swing-editor: It is our main/parent project. It has the following dependencies
a) junit3.8.1 b) swing-ext c) java-ext
junit3.8.1 is needed for the test purpose only. So the main dependencies can be shown as:

2. swing-ext: It contains some general Swing components and utilities for Java Swing. It depends on a) junit3.8.1 and b) java-ext. Hence the compiled jar entry look like:

3. java-ext: It mainly depends only on Apache commons-lang-2.1 except junit3.8.1 that is again only for testing. So the dependencies tree is:

Now if we see the whole picture of all dependencies, it will be like this:

We will see how to make the required jar using the pre-built assembly descriptor first. Then we will go for our own assembly descriptor and including the descriptor in the project POM file.

1) Using pre-build assembly descriptor:
POM file for "swing-editor":

<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.swing-editor</groupId>
<artifactId>swing-editor</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>My Company Swing Editor</name>
<url>http://www.mycompany.com</url>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.mycompany.java-ext</groupId>
<artifactId>java-ext</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.mycompany.swing-ext</groupId>
<artifactId>swing-ext</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.1</version>
<configuration>
<finalName>swing-editor</finalName>
</configuration>
</plugin>
</plugins>
</build>
</project>

Now you can make the jar by giving the following command:
mvn assembly:assembly -DdescriptorId=jar-with-dependencies

The pre-build assembly descriptors of Maven2 are here. If you see the "jar-with-dependencies.xml" you will find that it's descriptorId is "jar-with-dependencies". The final jar is built with the name "swing-editor-jar-with-dependencies.jar" i.e. Id appended after the value of "finalName" tag in our main project POM file. Now if you want to make the jar executable then you have to mention the main class file name in the main POM file. In that case the assembly section of our main POM will be changed like:

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.1</version>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
<archive>
<manifest>
<mainClass>com.mycompany.editor.Main</mainClass>
<addClasspath>true</addClasspath>
</manifest>
</archive>
<finalName>swing-editor</finalName>
</configuration>
</plugin>

2) Using custom assembly descriptor:
Now we write our assembly descriptor file. My descriptor file "assembly.xml" is more or less the copy of default descriptor file
except the "id" tag value has been changed.

<assembly>
<id>all</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>target/classes</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>

The required command in this case is:
mvn assembly:assembly -Ddescriptor=assembly.xml

Look my descriptor file is in "swing-editor" directory which is the project directory also. If you want to keep the assembly descriptor file in other location, then you have to mention the full path:
mvn assembly:assembly -Ddescriptor=/assembly.xml
You can customised the assembly file so many ways. The full descriptor file format is here. Customised it as your need.

3) Including descriptor in our POM file:
If you want to include the assembly descriptor file in your POM, then you have to modify the configuration section of your POM file as:

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.1</version>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
<archive>
<manifest>
<mainClass>com.mycompany.editor.Main</mainClass>
<addClasspath>true</addClasspath>
</manifest>
</archive>
<finalName>swing-editor</finalName>
</configuration>
</plugin>
In this case you just need to mention the "goal" in the command line:
mvn assembly:assembly

Wednesday, January 17, 2007

Making with Maven2 machinery | Part1: configuration & making executable jar

I've mentioned in my previous blog that Maven2 is my favorite build tool. I love it because of it's
object oriented approach in building projects; better to say it's philosophy to build projects. Moreover Maven2 is not only just a simple build tool, it's more than that. I will discuss them one by one in my blog.
Now the "getting started", "5 minutes tutorials", "basic examples" etc. all are available in maven site or some of the blogs. but I have not fount any link that describes full build configuration of Maven2, working examples that are really useful in production environment. I will try to give you some tips which can be helpful for the people who build projects for living.
Well, I'll not going to give you any introduction of Maven2; "what maven is", "what can be done with Maven2" etc. In first part, I will discuss about maven2 configuration and creating jar, executable jar with Maven2.

Tip1: Maven2 configuration :
Settings and configuration of Maven2 can be customized "globally" in "setting.xml" file in your M2_HOME\conf directory. Here "globally" has special meaning that the setting can be changed "locally" in your project module that override the "global" setting. Now I often confused with the local repository. If you are the first time Maven2 user, then you may face the difficulty to find the local repository. Maven2 makes the local repository in your home directory by default e.i. in Windows operation system it is under "C:\Documents and Settings\" directory. If you see the "settings.xml" file, you will find that the default value of this is: ~/.m2/repository. Hence Maven will make .m2\repository in your home directory. But sometimes this makes problems. I think it is better to set the local repository some other drive than C: such as D:\DEV\m2\repository. It's more convenient to view all the jars here rather than default directory.
How to do that?
Just add the following tag just below the default repository tag:

<!-- My local repository -->
<localrepository>D:/DEV/m2/repository</localrepository>

and make sure that the other repository tag/tags disabled.

similarly you can set the proxy setting, server and profiles. the best way to change some setting is to copy the whole tag, paste bellow the original, change as necessary and then comment out the original. In this way if you make anything wrong, then you can revert back the thing. For example you want to change the proxy setting, now search for under tag, copy the whole tag as:

    <proxy>
        <id>optional</id>
        <active>true</active>
        <protocol>http</protocol>
        <username>proxyuser</username>
        <password>proxypass</password>
        <host>proxy.host.net</host>
        <port>80</port>
        <nonproxyhosts>local.net,some.host.com</nonproxyhosts>
    </proxy>

just below the original one. Now change the values like:

    <proxy>
        <id>optional</id>
        <active>true</active>
        <protocol>http</protocol>
        <username>username</username>
        <password>password</password>
        <host>host IP</host>
        <port>port</port>
        <nonproxyhosts>local.net,some.host.com</nonproxyhosts>
    </proxy>

make sure the unnecessary part is commented like:

<!--
    <proxy>
        <id>optional</id>
        <active>true</active>
        <protocol>http</protocol>
        <username>proxyuser</username>
        <password>proxypass</password>
        <host>proxy.host.net</host>
        <port>80</port>
        <nonproxyhosts>local.net,some.host.com</nonproxyhosts>
    </proxy>
-->

Tip2: Making executable jar without dependencies:
It is very simple tip for creating executable jar for your project. But there is a drawback. It can't make executable jar for your project if your project depends upon other jar/jars. It can be done other way. I will discuss that in another blog entry.

Well, the main thing you need is "maven-jar-plugin" and you will have to configure this plug in properly. you will have to mention the main class for your projects i.e. the class that contains the main method like:

    <plugin>
        <groupid>org.apache.maven.plugins</groupid>
        <artifactid>maven-jar-plugin</artifactid>
        <configuration>
        <archive>
        <manifest>
        <mainclass>com.yourcompany.pack.Main</mainclass>
        <addclasspath>true</addclasspath>
        </manifest>
        </archive>
        </configuration>
    </plugin>

If you don't need the executable jar, then you don't need the above mentioned plug in anymore, just don't include the plug in.

Here are some nice links for Mave2:
1. Maven2 home.
2. Free book: Better build with Maven
3. IBM developerworks: Introduction to Apache Maven2

Sunday, January 14, 2007

New Year Wish

First of all, a very happy and prosperous new year to you, though it's belated. This is my first blog entry in 2007. Last year was so much eventful for me, good as well as bad. Last year I started writing blog, inspired by a lot of nice people here and there. Actually I started using internet last year heavy; this was very important for my digital life. I started using various online services like Google doc, Google calendar, Orkut and lots more as much as I can. Now I am too dependent upon these, I use off line software only when I don't get any online alternative for that. I use Orkut for online for community, friends etc, I use Google doc as alternative to off line office suite, I use Google calendar for all my planning, I use flickr and Google picasa for organizing my photos. I use del.icio.us and Google bookmarks for managing bookmarks. Even when I find any trivial importance for any type of file I tried to upload it for future reference. Recently I started using zoho online office suite which is even better than that of Google.These the ways my online experience changed.
I am mentally preparing for lots of things to do, to learn and "TO DO"s in the year 2007.

  1. Windows Vista and Java 6: I'm very mush eager to keep my hand in Windows Vista. It's going to be released on 31st January. I want to run my new Java Swing application written in Java 6 desktop advances on Vista, I also want to make my application look like native vista but written in Swing. I already have a taste of Windows vista beat2 on my machine running Java 6, but that's beta nahh.
  2. J2EE 5: I am very much unfortunate till now that I didn't get any opportunity to work in large scale, multi module J2EE application. If I say "I know J2EE" then that will surely make no sense, it's huge. I want to dig into this area as much as I can this year. More specifically I am interested in EJB3; how annotation makes wonderful coding in EJB3. I want to use JBDC 4.0 new features in my code. I also wish to use JPA. I wish to begin my own J2EE projects for learning purpose, I build multiple module and wish to design how they interact with each other. Maven2 is really a beautiful build tool. I saves lots of my time and I'm now very much comfortable with using Maven2. When your project consists of multiple modules and you need several jars here and there then Ant will do badly for you. If I can grasp J2EE deeply then I wish to write some reusable component in J2EE for general use. I believe in Separation of Concert in software development, so I wish to make multiple module and make them interactive among them.
  3. AJAX: This is the most hyped programming model among web developer now a days. I wish to learn what AJAX is for and what not. Cross-browser compatibility is very big issue in AJAX as far as I know (i don't know AJAX very well till now) I want to explore that. I wish to taste various AJAX frameworks available today and see how they handle all problems related to cross-browser compatibility .I wish to use POJO and DOJO too.
  4. Flex2: I also wish to learn flex2; how it's different from AJAX or similar to AJAX. Which one is more easy to learn and use. May be it will be better idea to learn them simultaneously. have you seen Google finance yet? It is built using Flex2.
  5. UML, design and refactoring: I want to go deeper with UML and I wish my software will be alike my UML desing. I wish to reflect that in my design and coding. Currently I use Altova UModel 2006 for my modeling. Although I'm quiet happy and comfortable with this, I wish to explore other UML tools also, if possible IBM Rational suite. Several code generation tool are available now that can generate code from your design, but I haven't use many of them. I like to use them for my code generation and refactor my code from within the tool.
  6. AOP: Aspect Oriented Programming attracted my attention a few days ago. AOP mainly deals with cross-cutting concert. Suppose you have hundred of thousands of class files across various packages. Now let you have to make a fix across all the classes or most of the classes, then it's not feasible to make change in all those files one by one, here AOP can help me. As am not quiet accustomed with AOP till now. I wish to learn it and to use in my code, project.
  7. Ruby and Rails: I'm not at all aware of these. I wish to learn these in 2007.
  8. C, C++ again: I wish to write code in my leisure if I can manage all other things.
  9. Functional Programming and Algorithm: I wish to go back to my college days when I started to write code in C. I can feel it's necessity but it's not very good for you if you want to develop for business. It's only food for my brain. I wish to read my old algorithm book, write code accordingly then find it's complexsity and improve it and calculate it's complexity and so on. Debasish da posted very nice entries for functional programing; really worth reading.
  10. Preparation of my own reusable software components: I have three software components in my hand:
  • a). java-ext: core Java extension for using Java generics, annotation more conveniently in your programme.
  • b). object-jdbc: it's very nice extension and implementation of Java JDBC API. If you use JDBC, then every time you have to deal with ResultSet and SQLException, but programmers can use object-jdbc object oriented way. Every database entry is object, not resultset. When programming with JDBC, a major problem arise when JDBC driver not work properly. I have used several databas driver for JDBC and I found many bugs in them. object-jdbc hanble these bugs to some extent now, I will try to handle all the bugs with in my code.This component use java-ext heavily.
  • c). swing-ext: some reusable swing components and utility.