Archive for the ‘Java’ Category

Software Requirements:
– Windows OS => WinXP, WinVista, Win7
– Eclipse v3.5
– JProfiler v5
– YourKit v8


A profiler is a very powerful tool to help pinpoint performance and memory issues. It offers a variety of ways to which a developer can help track down classes that lead to dismal performance, inefficiency and memory leak issues. This includes taking and comparing snapshot at certain point in time, watching how CPU time and memory is consumed in realtime in a visual chart and marking interested classes for analysis.
There are many profilers in the markets , both open-source (eg VisualVM that bundled with Java SDK) and commerical offerings. However, this document will show the steps to configure two of the best commerical Java profilers in the market, YourKit and JProfiler, to profile GWT client application in Eclipse in the Windows environment.
Even though both profilers are just as effective and powerful, both use different approach to profile the application.

Still, the following steps will work with any IDE (eg Intellij ) since it is just a matter of adding adding additional profiler’s agent as argument to the JVM. All IDEs allow argument to be passed to the JVM that run the project application.

Yourkit and JProfiler offer a 15 and 10 days evaluation trial respectively.



Read Full Post »


Final variable is used to define constant value and reference, and can only define once. In most cases, declaring a final variable is just a matter of assigning to a primitive value or a object reference directly.  However, in some cases, declaring a final class/instance variable may be more involving especially it get assigned from method that throws Exception (eg API for database access, web access), or it may involve multiple statements. In such cases, the code may become cluttered with class helper method and dummy temporary variable that help define final variable, making the code look less elegant and harder to maintain. The following common ways of declaring a final variable in those situations might look familiar to many.


Read Full Post »

Important: This version is not the official release by the original author of NanoXML (http://nanoxml.cyberelf.be/). It appears that the development for NanoXML has ceased.

Issue in NanoXML

Back in 2008, a version of NanoXML v2.2.4 is “unofficial” released to add comment feature (Javalobby mirror). However, there is one problem that persists which prevent developers from using NanoXML to manipulate xml file. The developers might use xml file for to store profile for their application and will need to manage xml file (etc delete, rename, add). However, it turns out that NanoXML is locking the xml file while waiting for the Garbage Collector to kick in and release the file. However the GC is behaving unpredictably and might not release the file as and when the program expects it. The developers will thus want more control over the release of file resource. Unfortunately, there is currently no API to force release of file resource under current release.

NanoXML v2.2.5

Therefore, I have modified NanoXML and released it as  NanoXML v2.2.5 to allow developers to use a specific method to release file resource manually so that the file can be manipulated (etc delete, rename).

  • public void close()
    This will release all file resource that are held up by Reader.

The aforementioned method will release the file resource that is created using the static method IXMLReader fileReader(String filename) in StdXMLReader class.


public class XmlTest

// ## means new features added.

    public static void main(String[] _args) throws Exception
        IXMLParser parser = XMLParserFactory.createDefaultXMLParser();

        /*// If pass string, use stringReader
        IXMLReader reader = StdXMLReader.stringReader(“<root></root>”);
        // Pass by file. Important to use toURL method otherwise exception will be thrown.
        IXMLReader reader = StdXMLReader.fileReader(
                new File(“c:/test.xml”).toURI().getPath());

        // parse() method does not include comment
        IXMLElement xml = (IXMLElement) parser.parse(true);   // ## true means parse comment too

        IXMLElement _x = xml.createElement(“newChild”);
        _x.setComment(“This is new child”); // ## Adding comment
        _x.setAttribute(“att1″, “me1″);
        _x.setAttribute(“att2″, “me2″);
        xml.addChild(_x, 0);  // ## Adding at specific position.

        IXMLElement _b = xml.getChildAtIndex(1);
        xml.removeChild(_b);  // Remove tag

        XMLWriter writer = new XMLWriter(System.out);
        // Default for write is excluded comment
        writer.setIncludeComment(true); // ## Include comment at generation.
        writer.write(xml, true);





Download NanoXML v2.2.5

The following file contains the full source code and the library jar file. To check on those changes that I have made, you can search for ‘GeekyCoder’ in the source code.

NanoXML-2.2.5.zip (93kb)

Released on 6 Feb 2010

Read Full Post »

image The truth is that there is no direct way to remove the maximize button off the  Resizable JFrame as it is created by Windows and it is not painted using Swing so U can’t touch this.

Still many posts on the net are giving “creative” suggestions on how to remove the maximize button of “JFrame”. The simplest and yet effective solution is to replace JFrame with JDialog as the latter does not have a maximize button. Other feasible “Java-based” solution is  remove the title bar and painstakingly implement customized title bar.


Read Full Post »

The objective of this post talk about my little “adventure” in resolving the fatal Java error that prohibits running Java application.


You use to have the Java applications and Java IDE on your Windows-based system working then one fine day, out of unknown reason, all the Java application and plug-in in the IDE cease to work. Worse, no error message and the application just silently fail to run. Oh, your first thought is that this shouldn’t happen because it is not even April’s fool day or Monday’s blue because today is Wednesday in July. So your first reaction is to re-install the Java SDK or runtime assuming that they might have been damaged. However just before re-installing you confirm that the SDK/runtime is indeed the main problem so you test-run some command in Window command console. jar.exe command run fine but running java.exe on directory other it’s bin directory produce the infamous

Error occurred during initialization of VM
java/lang/NoClassDefFoundError: java/lang/Object

What so damaging about this problem is the some plugins like Android and Grails in Intellij become non-functional as well. You know why very soon.

So here I go trying to resolve the issue. Running Java.exe produce error in other directories (since the SDK’s bin is added to path.)


However running in it’s bin directory, it runs fine.



Read Full Post »

Since Java 1.1, the recommended and standard way of event handling in a GUI component is through registering event listener. There are two types of event listener. One is the standard listener that are inherited in all component due to inheritance and the another is those specific to the UI type (eg Windows)

For example, the common types of event listener prevalent in all subclass of component include

  • ComponentListener
  • FocusListener
  • MouseListener

For those who want to understand more about event model of Java, do read http://mindprod.com/jgloss/event11.html

The use of event listener together with anonymous inner class greatly simplify event handling because registering of event listeners is just as easy as removing them through a series of add/remove event listener method defined in the component.

JFrame _fra = new JFrame(“Testing”);

_fra.addWindowListener(new WindowAdapter()



On the other hand,  as much as it make it easy to handle event, it make it troublesome to disable them especially event listener is registered throughout the component in various class. Each class may need to add method to enable/disable the event listener. There are likely multiple classes registering the same event in a component and all of sudden enable and disable them become involving and time-consuming. So what is the feasible solution to this situation ? The answer is to control and to use the centralized base class event methods that fire the specific event. By controlling these methods, it will be easy to enable/disable event firing even though the component might be registered with a specific event all over the classes.


Read Full Post »