Sunday, December 23, 2007

jhat - Java Heap Analysizer

Since I get involved in some jhat related project with one of my senior team members. I decided to make some blog entries. jhat is java heap analysis tool which is shipped in jdk6. There is an independent project name hat on jhat is flavor of this project only.

If you are a guy, who write lot of Java code and keep on looking for optimization, if you are getting errors like memory overflow.. then yes its a tools for you only.

jhat is all about giving readable information from java heap dump. Java Heap Dump contains information like how many classes, how many instances, execution flow and many more.
But all these information are in a binary format. And the duty of jhat is to read that file and provide useful information to you.

There are lot of ways to generate heap dump:

1. From normal java execution program, you can see the heap by Ctrl + Break.
2. jdk6,5 is coming with some tools like jmap, jconsole which help us to generate heap dump. Usages of these tools are very easy.
3. jdk5 also comes up with a tool called HPROF which can be used to generate heap dump.

So, here are the steps, how to approach the problem:

Make heap dump with jmap:

1. Run the java program, say SwingSet2 in the example.
2. Open other terminal, and run jmap
jmap -dump:format=b,file=heap.bin (On Windows, you can get pid by Ctrl-Alt-Del -> Process -> PID )

jmap will generate a file with name heap.bin which contains heap dump. Now pass this heap.bin to jhat.

3. jhat heap.bin

There are lot many optional argument, check the detail here.

Open browser, type: http://localhost:7000 (7000 is the default port), you can see hell lot of information. Grab the information of your interest. Or if you want to parse the information from yourself use OQL(Object Query Language) and write query.

Sunday, December 09, 2007

How Java handles Method ?

In my previous blog "Performance - Final Keyword ? " One of the blog readers asked question about How Java handles overriding ! So, I decided to make one entry(I guess more than one) for the answer.

Yes overriding tactics in Java is very different from C++ as methods by default in Java can be overridden unlike C++. In C++, the concept of overriding functions are handled by Virtual Table, VTable(This wiki link contains lot of information). Whereas in Java there is some other concept. Before going into the depth, let's see some of the basic things which one should need to know before making hand dirty in overriding concept.

Here is a Simple HelloWorld Program:

class Hello {
public static void main(String[] args)
System.out.println("Hello Bloggers!");

Lets see what the bytecode is generating, javap -c Hello(more about javap)

Compiled from ""
class Hello extends java.lang.Object{
0: aload_0
1: invokespecial #1; //Method java/lang/Object."":()V
4: return

public static void main(java.lang.String[]);
0: new #2; //class Hello
3: dup
4: invokespecial #3; //Method "":()V
7: astore_1
8: getstatic #4; //Field java/lang/System.out:Ljava/io/PrintStream;
11: ldc #5; //String Hello it is
13: invokevirtual #6; //Method java/io/PrintStream.println:(Ljava/lang/Str
16: return


Have a look on these lines:

1: invokespecial #1; //Method java/lang/Object."":()V
4: invokespecial #3; //Method "":()V
13: invokevirtual #6; //Method java/io/PrintStream.println:(Ljava/lang/Str

These are the lines related to method invocation. So what the heck is this invokespecial and invokevirtual ? Actually JVM used 4 different kinds of instructions for method invocation those are :

- invokevirtual - This is for instance method like System.out.println("Hello Bloggers!") here.
- invokestatic - This is for class methods.
- invokespecial - This is for special things. It is used when
- call , instance initialization.
- super call, when you will call something from super.method
- private methods. As private methods can't be overridden so we need to put this in a special category.
- invokeinterface - invoking instance method with interface reference(Soon we will see the example)

Now we are very clear that why invokespecial has been used at #1 and #3 whereas invokevirtual at #6. Ok, lets write some code which can see the usages of all four.

interface interfaceForHello {
public void noUse();

class Hello implements interfaceForHello {
public void noUse() {
System.out.println("No use");
public static void staticMethod()
System.out.println("Static method");
public static void main(String[] args)
interfaceForHello iface = new Hello();
System.out.println("Hello Bloggers ! ");

And here goes the javap -a Hello:

Compiled from ""
class Hello extends java.lang.Object implements interfaceForHello{
0: aload_0
1: invokespecial #1; //Method java/lang/Object."":()V
4: return

public void noUse();
0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #3; //String No use
5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: return

public static void staticMethod();
0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #5; //String Static method
5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: return

public static void main(java.lang.String[]);
0: new #6; //class Hello
3: dup
4: invokespecial #7; //Method "":()V
7: astore_1
8: aload_1
9: invokeinterface #8, 1; //InterfaceMethod interfaceForHello.noUse:()V
14: invokestatic #9; //Method staticMethod:()V
17: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
20: ldc #10; //String Hello Bloggers !
22: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
25: return


In next blog we will continue the same.

Saturday, December 01, 2007

OpenJDK Build on Netbeans with Windows / Solaris - Part II

Talking of some of the details about OpenJDK building(on Netbeans), we can start with Windows build(x86). In general,building on windows is tougher than Solaris/Linux because there are lot of dependencies on other tools and we can't set up the tools on network. Now lets see the list of the downloads required (don't abuse me, I know its too much :-))

- OpenJDK - Download open source JDK

- Binary Plugin - Download Binary Plugin

We need to install binary of Java SE 6, which we can get from Sun site and Java SE 7 which we can download from

Now this is all about JDK downloads. Time to put hand into the dirty world of other dependent tools:

1. As we have lot of C++ code(native code) inside JDK, we need a C++ compiler. For windows, JDK document suggests to use .NET2003. Though we have made the build on .NET 2003 but I guess compiler of .NET 2003 is free on net and you can give one try with free compiler. Note here: Don't try with .NET 2005 as many of the API get deprecated.

2. Cygwin Installation: This is a area of concern. We have invested(wasted :D) 90 percent of the build time in this tool. Installing, uninstalling, reinstalling .. and it keep on going. On the first installation page of Cygwin itself , you will get two options: Unix/Binary or Dos/Text. Any option can work, can't work depending on your luck :-). Most of the engineer suggested me to install Unix/Binary but Dos/Text worked for us :D. Download from: Cygwin Site

NOTE: Don't forget to download Developer, System, Utility, Archive package of its full mode not the default mode.

3. Install Microsoft DirectX 9.0 SDK(Summer Package) - Download it from Microsoft site

4. Download and Install FindBugs and Ant

5. Download Microsoft Unicode Library

6. Download and install latest version of freetype from

7. Oops, if you don't have Netbeans 6 then download that also :)

Time to take a cup of coffee. Let the download to go on.

OK, back from coffee ...

Add Ant, Findbugs, Cygwin(till \bin) in the path variable of system. Open cmd, run vsvars32.bat (Micros~1.NET\Common7\Tools\vsvars32.bat) to initialize the .NET variables. Run Netbeans.exe from the same command line.

Open openjdk/jdk/make/netbeans/ in netbeans and open project jdk. Open some of the important file like build.propertied and make.xml. In make.xml file, you will get the path of make, change it to the cygwin make file {cygwin/bin }.

Now IMPORTANT, get ready to write file:

// this is important because code contains many
//line which work on if(OPENJDK==true) code
// write the path in ~ format (dir /x)
ALT_MSDEVTOOLS_PATH= {MS.NET2003_PATH}/Common7/Tools/Bin \

All Done ! Fire the build now, if you are lucky enough you will get it done in some 20-30 minutes. In the next blog, we are going to talk about the problem(s) with some of this software and version dependencies.

OpenJDK Build on Netbeans with Windows / Solaris - Part I

Our team is ready with OpenJDK build for FOSS.IN, better to say partially ready :). Me and Vikram is going to give the live demo about building OpenJDK and how you can optimize the source code according to your own need. For Example, if you don't like the default LookAndFeel of Java, change it according to your like because now its your world, its your Java. Let me show you one simple code change we have done with the gradient color. This is the normal Ocean LookAndFeel of Java.

Before the code Change:

After the code change:

Though building OpenJDK is little cumbersome process but come on, its a one time process. Just make the build ready once and then screw it as you want both on the Java level and on native level . Even more process and standard documents will come as OpenJDK goes into its maturity stage. You will love to know that Netbeans 5.5+ is giving a high class support for making OpenJDK across all platforms.

So for the real die-hard of Java, I am going to write Part II, III, ... upto N(covering some technicalities of building process) till I am not able to make you happy. Thanks to Igor and Sandeep who have replied my all shots of stupid questions.

Thursday, November 29, 2007

Performance - Final Keyword ?

Last night, me and Abhishek was discussing about some performance issue. Finally we came on a good conclusion and so I decided to write a blog. One of the Orkut users posted a question on our community

"As we know that by declaring method as final, we cant override them..........By performing this function, it provides a performance enhancement.........but i unable to understand how??????????? can anyone help me out...... "

(I don't know why people are using so many of dots and question marks)

Yes this is correct, writing final (can) improve the performance. Straight away question - WHY ?
When you define a method final, that means it can't be overridden into its derived classes. Knowing that into the fact complier (can) inline that method into its derived classes. But but but, this is not a safe way to inline a method. WHY AGAIN ?
Because, if by any chance if method will not be final (fool the VM) in base class, then inline entry in derived class will be wrong and things can be screwed. Now very rare but this is possible in Java. Remember in Java you can write your own classLoader and you can make a classLoader where you can ignore(any how) the final declaration.
So, for those compiler in which final method be inlined before loaded into VM(unlike JIT), there is quite a high risk and hence compiler can't do this. But in JIT compiler, this is what is happening ! (If you want to know more about JIT, please let me know :) ).

And this is how we finally ended our discussion :)

Sunday, October 21, 2007

SplashScreen for impatient Users :)

One more API in Java for those who are impatient in handling Java Application. A heavy application in Java takes time before starting up and hence JDK6 comes with a concept of SplashScreen. Its a simple class in JDK6 where user can show a screen on top of his application screen(before it getting start) that ,be patient, application will run in few seconds. Good part, it also supports gif image, so for completely impatient user who generally starts clicking here and there :D and screwing application, show a GIF image of progress bar.

It comes in various flavors:

- SplashScreen stay on the desktop till the main screen(application UI) will not come. Some polling or signal mechanism internally, I never bothered to see the code :D

- SplashScreen stay for given amount of time. A simple one.

Running is not an easy job. You have to define splash option with java

java -splash:imagename classname

I guess a good API for those who make games or some big application. One can give information like Copyright, Developer name, Loading..., Company name, etc etc. I mean generally for these information we waste one screen in our main application itself, why not use SplashScreen for that :).

You can see some sample code here:

Wednesday, October 10, 2007

Java Font vs Native Font

So, here is one more problem of Java being machine independent. Font size depicts by Java Application will not be same as of Native Application, like if we consider Windows OS, native applications are MS office, Notepad and many more. Conversion from the size in points into device pixels depends on device resolution as reported by the platform APIs. Java 2D defaults to assuming 72 dpi.Platform defaults vary like Mac OS: 72 dpi, Linux/Solaris: 96 dpi, Windows: 96 dpi/120 dpi.So whenever you write a code for java font, be carefully of it's inconsistency with native application.

Look at this code, how to manage the font size on a particular platform(pretty simple)

import java.awt.*;
import javax.swing.*;

public class TextSize extends JPanel {
public void paint(Graphics g) {
Font font = new Font("Arial", Font.PLAIN, 30);
g.drawString("Hello World", 25, 100);
Toolkit tk = Toolkit.getDefaultToolkit ();
int nativeResolution = tk.getScreenResolution();
final double javaResolution = 72.0; // java default is 72 dpi
Font font_new = new Font("Arial", Font.PLAIN, (int)Math.round(30.0*nativeResolution/javaResolution));
g.drawString("Hello World", 25, 150);
public static void main(String[] args) {
JFrame frame = new JFrame();
TextSize tx = new TextSize();

One can see the difference of Java and Native font size difference. The first "Hello World" is normal java code. Second " Hello World" is modified java code and third one is Native "Hello World". The staircase problem is also visible in Java Font, for removing that we can set ANTIALIAS_ON :-)

Friday, September 28, 2007

Synchronization - Wait a min.

Somewhere platform independence of Java fails when it all comes on Threading. Not a fault of Java because Threading means lot of dependency on system. How a particular OS is going to handling with your thread. Like if we see Windows NT, it doesn't support priorities of thread like priorities of Process, whereas Unix based system gives you the freedom to prioritize both thread and process.
Anyway, I am writing this article to just say that take care whenever you are writing any synchronized call. Every synchronized block or a method has an overhead and we can't ignore it. So before going to synchronization there is something called atomicity which means atomic operation like int a =2; luckily we never have to worry of threading issue in atomic calls. Its all thread safe. Again, take care x++ or x+=y is not falling under this atomic operation because there are two things going-increment and assignment.

Now, see the code here and also overhead of sync. call, which is of no use here:

import java.util.Date;

class ThreadCheck {

synchronized float uselss(int a, int b, int c)
return (a+b+(c/2));

float useful(int a, int b, int c)
return (a+b+(c/2));

public static void main(String[] args)
ThreadCheck threadcheck = new ThreadCheck();

//lets check the time
double begin = new Date().getTime();
for(int i=0;i<50000;i++)
double end = new Date().getTime();
System.out.println("Time taken by Sync. call: " + (end-begin));

begin = new Date().getTime();
for(int i=0;i<50000;i++)
end = new Date().getTime();
System.out.println("Time taken by non-Sync. call: " + (end-begin));

This is the output I am getting:

E:\Program Files\Java\jdk1.6.0\bin>java ThreadCheck
Time taken by Sync. call: 16.0
Time taken by non-Sync. call: 0.0

One can clearly see a big overhead here. So the idea is just to take care while writing sync. block because threading is more in demand when we write real time systems and in RTS we can't afford such a wastage of time :).

Friday, September 21, 2007

Running Batch files in parallel

Yesterday night, one of my roommates got one problem. Actually his application demands to start 3-4 servers and all the servers has a startup.bat file. So, he actually want to write a code which can run those batch files. Now the problem here is he want to execute all those bat files in parallel. Note that a content of bat file always run sequentially. He was looking for any code which can do that, but we have selected java to do so :).

Runtime class has power to execute any batch file and threading concept has the capability to run batch files in parallel. So here goes the code:


public class BatchRunner extends Thread {

String msg;
public BatchRunner(String s) {
msg = s;
new Thread(this).start();

public static void main(String args[]) {
new BatchRunner("Hello");

public void run() {

String line = null;
Runtime r = Runtime.getRuntime();
try {

FileReader fr = new FileReader("D:\\Batchfiles.txt");
BufferedReader bufferedReader = new BufferedReader(fr);

while((line = bufferedReader.readLine()) != null)
} catch (Exception e) {
System.out.println("Error in Execution");

Batchfiles.txt contains full path of those batch files, which need to be executed. This is a fast solution and code must be optimized here.

Monday, August 06, 2007

Focus - lot to do ..

After JDK 1.4, there was a lot to see about focus in Java. Its one of the most interesting and challenging topics in AWT/Swing. JDK1.4 onwards there is one central class which takes care about the focus on components/windows KeyBoardFocusManager. Not only that, now you have freedom to write your own FocusManager.

This is one example where I tried to catch the focus lost and gain event on Focusable components.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class ButtonTest{

public static void main(String[] args)
ButtonFrame frame = new ButtonFrame();

class ButtonFrame extends JFrame {
public ButtonFrame()
setTitle("Button Test Frame");
ButtonPanel panel = new ButtonPanel();

int LENGTH = 300;
int WIDTH = 300;


class ButtonPanel extends JPanel implements FocusListener {

public ButtonPanel ()

JButton button1 = new JButton("Button1");
JButton button2 = new JButton("Button2");
JButton button3 = new JButton("Button3");
JTextField text1 = new JTextField("Enter here");


public void focusGained(FocusEvent e) {
display("Focus gained", e);

public void focusLost(FocusEvent e) {
display("Focus lost", e);
void display(String prefix, FocusEvent e) {
System.out.println( prefix+" " + e.getComponent().getClass().getName());

If you are working on AWT or Swing , I would personally prefer to visit this article of JavaWorld:

Saturday, July 14, 2007

Robot Class

Robot class is one of the most common classes used by guys doing automated testing. Giving some cool features to automate process. But still I find some thing lacking in this library or maybe I don't know how to use them.

Here I try to write a simple code, which write some text on a text editor and then close the editor without saving the file. Many things are hard coded in this example, like

robot.mouseMove(1420,20); // this is my desktop close button place in full screen mode.
So, if you want to run this code, just change this line according to your desktop. After changing run this code, open any editor say notepad within 10 seconds and then this example will write some text on it and then it will close the window.

import java.awt.Robot;
import java.awt.event.*;

public class MainClass {

public static void main(String[] args) throws Exception{

Robot robot = new Robot();
// giving you 10 sec to open any text editor.

robot.mousePress( InputEvent.BUTTON1_MASK );
robot.mouseRelease( InputEvent.BUTTON1_MASK );
robot.mousePress( InputEvent.BUTTON1_MASK );
robot.mouseRelease( InputEvent.BUTTON1_MASK );


This is some useless example, but one can thing of some good examples but using this class.

Saturday, July 07, 2007

jikes vs javac

In my last post Priyanshu made a comment, " have you tried jikes". So, decided to give a try.

jikes is something which converts .java file into .class file. Yes, it is javac but IBM product not a Sun product. And let me begin with jikes page on :

"You may wonder why the world needs another Java compiler, considering that Sun provides javac free with its SDK. Jikes has five advantages that make it a valuable contribution to the Java community:".

Providing free things, softwares and codes is a big deal for IBM but never for Sun. So don't keep on singing this Open source contribution song all the time. Whole world know what IBM is giving free.

So, advantage(s)

No. 1 : jikes is a open source software.
And so javac is ! Didn't get the advantage.

No. 2: Strictly Java compatible - It is not providing any superset or subset variation support.

Good, but this little of strictness most of time increase the code line from thousands or so.

No. 3: High Performance: Compilation is faster than javac.

Who really bother about compilation, anyway you have to come again and run on java command. I guess world is ready to devote 2 more minute in compilation.

Anyway, I have compiled one of the .java file(one which i have used in the last post) on the most stable version of jikes:


Issued 5 system warnings:

*** Semantic Warning: The file "E:/Program Files/Java/jdk1.6.0/jre/lib/ext/" doe
s not exist or else is not a valid zip file.

*** Semantic Warning: The file "E:/Program Files/Java/jdk1.6.0/jre/lib/ext/" doe
s not exist or else is not a valid zip file.

*** Semantic Warning: The file "E:/Program Files/Java/jdk1.6.0/jre/lib/ext/" doe
s not exist or else is not a valid zip file.

*** Semantic Warning: The file "E:/Program Files/Java/jdk1.6.0/jre/lib/ext/" doe
s not exist or else is not a valid zip file.

*** Semantic Warning: The file "E:/Program Files/Java/jdk1.6.0/jre/lib/ext/" doe
s not exist or else is not a valid zip file.

Didn't get what this warning message is and really not why you show it to me 5 times.

Then I have tried to look into -version command.

javac -version

- javac 1.6.0

jikes -version

- Jikes Compiler - Version 1.22 - 3 October 2004
Copyright (C) IBM Corporation 1997-2003, 2004.
- Licensed Materials - Program Property of IBM - All Rights Reserved.
Originally written by Philippe Charles and David Shields of IBM Research,
Jikes is now maintained and refined by the Jikes Project at:

Please consult this URL for more information and for reporting problems.

Very informative :)). Originally written by ... what does that mean ?

No 4. jikes provides some cool feature like incremental build.

I didn't get anything out of it.

Look at this feature.

jikes ++

// After the 5 warning message.

Incremental: Enter to continue or q + Enter to quit:

nothing changed...

Incremental: Enter to continue or q + Enter to quit: (this if you change the code)


Incremental: Enter to continue or q + Enter to quit:

nothing changed...

No 5. clear error message.

I have removed one semi-colon from the code at line no. 25. Look at the error message printed by two compilers:

javac ';' expected
stringJDB = value
1 error


Found 1 syntax error in "E:/Program Files/Java/jdk1.6.0/bin/":

25. stringJDB = value
*** Syntax Error: ";" inserted to complete BlockStatementsopt

Are you able to find out something extra ? By the way what was the meaning of BlockStatementsopt ?

I guess I made too many of negative statement :), Ok some good features are here:

- it is providing some additional options like

> jikes +Punused-package-imports - which at compilation time, will show you those import statement which are not in use.

> jikes +Pnaming-convention - it will throw warning if you are not following the naming convention ( its really an idea of Sun, don't mind :) )

Again, I didn't like the verbose information(javac -verbose . Where is the time information man... parsing time, total time to make class file... these are some of the cool use of verbose option. Showing only which all files you are reading is a useless information.

Anyway, nice to see one step ahead in the world of open source.

Sunday, July 01, 2007

xjc.exe ?

Today I was looking some options in jdk/bin and suddenly I came across a file name xjc.exe in JDK6(I guess Mustang is no more official name of JDK6). Quickly I checked the existence of this file in JDK 5.0 and JDK 1.4.2. File was not present. Goggling didn't provided too much of help and I tried with myself :)

I just copied an XSD file from somewhere:

FileName: unknowntest.xsd (some problem with blogger, not able to take xml code :D, so pasting the image)

and passed this XSD file into xjc option:
Usage: xjc [-options ...] ... [-b ] ...

> xjc unknowntest.xsd

parsing a schema...
compiling a schema...

Ah I got 2 Java files yeppp - and
Look at these 2 files ( I am removing the documentation part of it, to make it small)

package generated;

import java.math.BigDecimal;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlType(name = "", propOrder = {
@XmlRootElement(name = "country")
public class Country {

@XmlElement(required = true)
protected String name;
@XmlElement(required = true)
protected BigDecimal pop;

* Gets the value of the name property.
* @return
* possible object is
* {@link String }
public String getName() {
return name;

* Sets the value of the name property.
* @param value
* allowed object is
* {@link String }
public void setName(String value) { = value;

* Gets the value of the pop property.
* @return
* possible object is
* {@link BigDecimal }
public BigDecimal getPop() {
return pop;

* Sets the value of the pop property.
* @param value
* allowed object is
* {@link BigDecimal }
public void setPop(BigDecimal value) {
this.pop = value;


and objectFactory don't have too much in it. Just creating a Country class instance. Oh so it is something cool, its making all the getter and setter method for me, what else I need :).

Remember, such type of methods are too much in demand when you are writing any jsp page. So, if you are clear with the data type, just make an xsd file and xjc will convert in into java code.

I still doubt what xjc refers for ... may be xml to java converter :)

Friday, June 29, 2007

Java Debugger

Since there is lot of high class debugger available with GUI, we never bother to use Debugger provided by JDK itself. Actually I also get to use of this someday back. Luckily lot of things available to use and some good options but some error message really wired you. But good if you want to look into those dirty threads and stack calls. Anyways lets try for some basics.

I have written a small code to debug(really don't have anything in the code to debug :) )

public class TestForJDB {

String stringJDB;

public static void main(String[] args)


TestForJDB test4jdb = new TestForJDB();

test4jdb.setString("test for JDB");

String getstring = test4jdb.getString();

String returnString = test4jdb.toString();


public TestForJDB()


System.out.println("Testing for JDB ");


public String getString()


return stringJDB;


public void setString(String value)


stringJDB = value;


public String toString()


return stringJDB ;



Now lets start jdb. Remember compile with -g option so that class file will get the debug informations.

E:\Program Files\Java\jdk1.6.0\bin>jdb TestForJDB

Initializing jdb ...


Putting some breakpoints. We have 2 ways either by line number or by method name.

> stop at TestForJDB:4

Deferring breakpoint TestForJDB:4.

It will be set after the class is loaded.

> stop at TestForJDB:11

Deferring breakpoint TestForJDB:11.

It will be set after the class is loaded.

> stop at TestForJDB:16

Deferring breakpoint TestForJDB:16.

It will be set after the class is loaded.

> stop at TestForJDB:20

Deferring breakpoint TestForJDB:20.

It will be set after the class is loaded.

> stop at TestForJDB:24

Deferring breakpoint TestForJDB:24.

It will be set after the class is loaded.

Now lets go ahead and run the class file

> run TestForJDB

It will show you some crappy information. Anyway, lets try to look into the methods available:

main[1] methods TestForJDB

** methods list **

TestForJDB main(java.lang.String[])

TestForJDB ()

TestForJDB getString()

TestForJDB setString(java.lang.String)

TestForJDB toString()

java.lang.Object ()

java.lang.Object registerNatives()

java.lang.Object getClass()

java.lang.Object hashCode()

java.lang.Object equals(java.lang.Object)

java.lang.Object clone()

java.lang.Object toString()

java.lang.Object notify()

java.lang.Object notifyAll()

java.lang.Object wait(long)

java.lang.Object wait(long, int)

java.lang.Object wait()

java.lang.Object finalize()

java.lang.Object ()

main[1] step


Step completed: "thread=main", TestForJDB.main(), line=6 bci=0

6 TestForJDB test4jdb = new TestForJDB();

Something unique to see here, bci = 0, what does that mean ? Don't know actually. BCI is Byte code Instrumentation and probably help if you want to go for native level debugging.

list command will tell you the current position of the debugger.

main[1] list

2 String stringJDB;


4 public static void main(String[] args)

5 {

6 => TestForJDB test4jdb = new TestForJDB();

7 test4jdb.setString("test for JDB");

8 String getstring = test4jdb.getString();

9 String returnString = test4jdb.toString();

10 }

11 public TestForJDB()

locals, a useful command will tell you the Methods arguments and local variable at that break point.

main[1] locals

Method arguments:

args = instance of java.lang.String[0] (id=331)

Local variables:

There are many other options like dump, step up which are useful for serious debugging. Quite Strange to know that such a tool was introduced in JDK 1.2. If you want to look into the architecture of JDB then visit this site:

Saturday, June 16, 2007

Monday, May 21, 2007

Java FX ... The X Factor !

Ah, so one more scripting language. It seems that we are going more and more lazy in writing code. Anyway, I am all set for JavaFX programing. So far looking cool. Downloading NetBeans solves most of the problem.

Setting up for Java FX: Here. First time saw anything that can be done easily in Java(because I guess its not Java :) )

And my Cruel World Program,

import javafx.ui.*;

var window = new Frame();
window.title = "Title Bar";
window.width = 400;
window.height = 200;
var label = new Label();
label.text = "Cruel World";
window.content = label;
window.visible = true;

By the way where is the class. Sir this is the world of Scripting :). I guess a easier way to write code with more smoothness and less tension of inheritance, abstract funda.

So, trying for a code, in which I will take my photo and zoom it. So Nice :D. Will comeback with code in next blog.

Sunday, May 20, 2007

Searching Impl.

This is one bad implementation of the last blog. Need to change a lot of part to run in generic way, I am too lazy :)

import java.util.*;
public class SearchFile
public static void main(String args[])
String osName = System.getProperty("" );
String[] cmd = new String[5];
if( osName.equals( "Windows Vista" ) )
cmd[0] = "cmd.exe" ;
cmd[1] = "/C" ;
cmd[2] = "dir *.java /s";
cmd[3] = ">";
cmd[4] = "Hello.txt";
/* else if( osName.equals( "Unix" ) )
find name filename


} */
Runtime rt = Runtime.getRuntime();
System.out.println("Executing " + cmd[0] + " " + cmd[1]
+ " " + cmd[2]+ " " + cmd[3]+ " " + cmd[4]);
Process proc = rt.exec(cmd);
int exitVal = proc.waitFor();
System.out.println("ExitValue: " + exitVal);
} catch (Throwable t)

Look at the search speed after first run. Its simple awesome. But it will create problem if user dont have permission to run a process.

Friday, May 11, 2007

How to get ?

I was trying to automate some of my manual work, I came across one problem. Is java providing any option by which if I give the file name and directory name it will go ahead and search that file in the given directory or sub-directory and return me the complete path of the file ? Unfortunately not :(, there is no direct API that can do this work for me. But Kannan told me that one can make a code which can recursively do this work. And he already had written some code for this. I was trying to tackle this problem with a different approach. The approach is something like:

On windows, dos gives me an option to search a file in a dir or sub-dir by command:
dir filename /s
Linux and Solaris gives me option of search the same by command: find /directory_name filename
So, why not using the runtime option of java system class and write a code like:

if ( OS == Windows) {get it by dir filename /s}
elseif (OS == Linux or Solaris or Unix){get it by find /directory_name filename}
else {God Knows only :)}
Right now I am not ready with code(because this dos is not giving anything directly, so need to remove useless things) but will come back soon with the results and the optimal code. Any suggestions are most welcome.

Saturday, March 31, 2007

Class file demand for which Java version

Someday back I got a call from my roommate's friend asking about that he has moved some class files from one system to other and now those class files are not running. I guess he got those class files from somewhere and he has no idea about which version of JDK it will run.

It's not such a tough job to find out that which version of JDK your class file is demanding. What we need to know is the format of class file and rest is like in your hands. Just look at the code for getting more clear picture.

class CheckClassFileFormat {
public static void main(String[] args) throws Exception
int magicNumber;
int minorVersion;
int majorVersion;
// and many more
DataInputStream data = new DataInputStream(new FileInputStream(args[0]));
magicNumber = data.readInt();
if(magicNumber !=0xcafebabe) // why 0xcafebabe.. read it, its so funny
System.out.println("Not a correct class format");
minorVersion = data.readShort();
majorVersion = data.readShort();
String version ="no version";

/*The Java virtual machine implementation of Sun's JDK release 1.0.2 supports class file format versions 45.0 through 45.3 inclusive. Sun's JDK releases 1.1.X can support class file formats of versions in the range 45.0 through 45.65535 inclusive. Implementations of version 1.2 of the Java 2 platform can support class file formats of versions in the range 45.0 through 46.0 inclusive.*/

if (majorVersion < 48) {
version = "jdk1.3.1";
} else if (majorVersion == 48) {
version = "jdk1.4.2";
} else if (majorVersion == 49) {
version = "jdk1.5";
} else {
version = "jdk 6";
// can follow up the same line for new releases.

System.out.println(args[0] + ": correct jdk version is " + version);
Just pass the classfile (like Test.class) in argument and we get to know on which JDK it need to run :)

Sunday, March 25, 2007

String vs StringBuffer vs StringBuilder

Let me first tell you what is StringBuilder. StringBuilder is a class analogous to StringBuffer added in JDK 1.5. This class is designed to use in place where StringBuffer is used by single thread(like in most of the cases). According to documentation, StringBuilder should work faster than StringBuffer. So " thread unsafe, fast".

I was reading one of the posts of orkut Java community asking "what is this capacity in StringBuffer and even we can add two strings from String class why to go for StringBuffer". Valid question ! GC need to work little more in case of String, but thats fair. As a java wellwisher let me try to do some publicity of Java API :D. And here it goes:

No don't use String class for concatenation operation, always use StringBuffer/StringBuilder and let me tell you why ?

This is a simple Java code for string addition in String and StringBuffer:

class StringTest {
public static void main(String[] args)
String s = "just a string";
s = s + "add me too";
StringBuffer s = new StringBuffer("just a string") ; //StringBuilder s = new StringBuilder("just a string");
s = s.append("add me too");

Alright, now have a look on the bytecode of this program(don't be panic).

>> javac
>> javap -c StringTest

Compiled from ""
class StringTest extends java.lang.Object{
0: aload_0
1: invokespecial #1; //Method java/lang/Object.
4: return

public static void main(java.lang.String[]);
0: ldc #2; //String just a string
2: astore_1
3: new #3; //class java/lang/StringBuilder
6: dup
7: invokespecial #4; //Method java/lang/StringBuilder.
10: aload_1
11: invokevirtual #5; //Method java/lang/StringBuilder.append:(Ljava/lang/
14: ldc #6; //String add me too
16: invokevirtual #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)
19: invokevirtual #7; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
22: astore_1
23: getstatic #8; //Field java/lang/System.out:Ljava/io/PrintStream;
26: aload_1
27: invokevirtual #9; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
30: return


Just see line no. 11. Interesting, the plus sign we used for addition is not as innocent as it looks. String itself use StringBuffer(StringBuilder) to add two strings and hence taking much more time than normal append operation done by StringBuffer. Let me give you more evidence, run verbose option and check the time

>> javac -verbose

and check the other one, that is, with StringBuffer one.

You can clearly figure out the time difference and make a try with StringBuilder, time should reduce furthermore.

So, how is my publicity :D

Tuesday, March 20, 2007

Bug Patterns in Java

An attitude of solving the problem when it aroses is always be so called " a lovely" behaviour of Software Engineers and so I. Most of us love this and never bother to read book :) . Anyway with a half-hearted mind, I start reading a book "Bug Patterns in Java". In the first glance name of the book suprised me becasue I never before saw a language book which discuss only about bugs. I liked the title and started the book. Also, as java becomes a mature product, a maturity of something around 11-12 years, resolving any bug of this language is a nightmare's job.

Author,Eric Allen started with a very macro level saying every bug falls under a pattern and if not then go ahead and make a pattern for it :). Let me tell you about one of the most common patterns. One pattern call "Rogue tile", the cause of this bug is we do copy paste of code, bug get fixed in the orignal copy and we forget to fix it into the copy paste code. So funny, but yes this is one of the most common errors we used to do.

So far, so good. Hoping for something great out of it :)

Wednesday, March 07, 2007

JDK 1.7 - Whats New ?

I remember the exact date when Mustang got released. Mustang is going great because of it's deadly features like JS support, Annotations and many more.

Moving forward to JDK 1.7-Dolphin which has a expected release in 2008! Whats new ?

I am making a complete presentation on Dolphin and will upload it soon ! Proposals are:

1. SuperJAR's - this is something really cool

2. Co-bundling with other language interpreter like JRuby, Jython.

3. Closures in java - something in one line we can understand local variables for a function - kept alive after the function has returned

4. More enhancement with Java Persistence API

and many more ...

The new JDK comes into a new flavor of open source, so get ready >

Tuesday, February 27, 2007

Pattern Class

Finite Automata ! Remember ! We used to make a lot of Automata at college time. Making Pattern is one of the most tricky jobs and we need to take care of lot of things.Best Part of the whole story is JDK 1.4.2 onwards support Pattern Class and believe me its simply awesome.
So, I decided to make a check for Class name, if a class name starts with Caps its a perfect one else not as per Java coding guideline. So, here is my simple class which I need to check :

class box {
int width = 0;
int height = 0;

Now look at the checks (all these are valid )

class Box
class (space) Box
(space) class
public class Box

So, Decided to make a pattern ".*class[ \t\n\f\r]+[A-Z].*" means anything before the String class is fine, there can be space,tab in between class and classname(more than one,so +) , classname should start with a Caps. I am not checking any other character except the first one, with the assumption that file is already a compiled class.

There can be a lot more optimized pattern and more you optimize,more you feel happy :). Here goes my simple,ugly written code that checks the coding standard(only for class name, and that even for one class only :D) file.

See hoe easy to make a option say java -checkstandard which checks for the correct coding guideline in .java file. And if its not according to coding guideline, we can throw the error and why to throw the error, we can replace it with right name(right according to Java Standard). So the code will change

class box {
int width = 0;
int height = 0;


class Box {
int width = 0;
int height = 0;

// Here goes the code //

import java.util.regex.*;

class ClassNameChecker {
public static void main(String[] args)
String line;
String sub;
try {
BufferedReader in = new BufferedReader(new FileReader(""));
line = in.readLine();
Pattern p = Pattern.compile(".*class[ \t\n\f\r]+[A-Z].*");
Matcher m = p.matcher(line);
System.out.println("Class Name follow Java Coding Guidelines");
System.out.println("Not a Java coding Standard");
catch(Exception e)
//something something

Tuesday, February 13, 2007

Next Decade is challenging ...

Revolution in Science is most unlikely in last 2-3 decade. I guess adoption of Internet was one and the only revolution of last few decades. But if we consider optimization, we did a great job ! From maintaining diary to writing blogs, from railway counter reservation to e-reservation. Very well depicted by this youtube video of Web 2.0

But I have a gut feeling that in the upcoming decade we are going to see some of the revolutions. And here I get the first news: "First Quantum Computer Tuesday". British Columbia-based D-Wave will demonstrate the world's first quantum computer this Tuesday. D-Wave is doing a good job in making Quantum Computers commercial but in spite of hard work they are fighting with Financial optimization.

Saturday, February 10, 2007

File Transfer ...

I was looking at all the possibilites of transferring file from one system(source system(s)) to other(target system) through pure java code. Socket programming is one of the ways suggested by my friend. But it seems to be a tedious job... opening connection, sending request, closing connection. And the base line truth is that I don't know how to do these all :).

I was searching in Java Library for file transfer and I saw Java FTP client Library support. We can upload or download files from remote computer running an FTP server simply with some method calls.

Look at this code:

FTPClient objectClient = new FTPClient();

FTPClient Package was supported by Sun, its never be a part of jdk package and I guess it was deprecated now :((
Any simple idea to do file transfer ?

Friday, February 02, 2007

Dead or Alive !!

Ahh this time its cool. Again a good conversion but this time I guess Java is going pretty cool. Work is to write a script (no script, program :D ) which checks the status(Dead or Alive) of Network machines. Yes an easy job for scripting language. Believe me, much easy in java as well. And here it goes:

public class MainClass {
public static void main(String[] args) throws IOException {
try {
int timeout = 2000;
BufferedReader br = new BufferedReader(new FileReader("machine.txt"));
String machineName = null;
while ((machineName = br.readLine()) != null) {
InetAddress address = InetAddress.getByName(machineName.trim());
if (address.isReachable(timeout))
System.out.println(address + " is Alive");
System.out.println(address + " is Dead");
} catch (Exception e) {
System.out.printf("Unknown host");

Thanks to my team member to add this reading file code. If you remove file reading lines, code is not more than 4-5 lines. machine.txt contains name of the machines like

I have compiled this code on JDK 1.5 but it should run on the lower version of JDK's as well (hopefully).

Tuesday, January 30, 2007

Running Javac from Java !

Oh Long time, No post .. So I decided to go for one :). I suspended the thread of JVM Part-N because some of my friends told that these things are also written in book, write something extra. For writing extra, I don't have knowledge :D

Anyway, as we all know Scripting language is the most powerful language for running real application and most of the time I get it challenging to convert that script code into Java code, but believe me I am always ready to take this challenge. So this time my challenge was to run set of Java codes from a single Java Program. In general we run java code by opening cmd and doing those javac,java.

Again, its a damn easy job for a scripting language, but ... Ah come on you are a java programmer. And here the result, though currently I am running one Java code only from other Java Program (sorry for writing the code in a bad way)


class JavaCode {
public static void main(String[] args1)
{ javac = new;
File file = new File("");
String filename = file.getName();
String classname = filename.substring(0, filename.length()-5);
String[] args = new String[] { filename };
int status = javac.compile(args);

(Some bad naming convention, some crap hard coding.. but you can pardon me for that :D ). Some unused part of code like taking classname, I am going to use it further to run the code. Ok this code is compiling and telling the status of ...some Helloworld code (please put both in same location :) )

Ok so next is to run and print Hello World ... Not a tough job :)

Sunday, January 07, 2007

JVM Internals – Part II (Space Organization)

(Click on the main title to go for Part I)

In my previous post, I forgot to write about JVM specification (mind it, it’s a specification, Sun implementation of this is HotSpot, there are many others). JVM specification is written by Tim and Frank (The JavaTM Virtual Machine Specification, this is a free download book written by them). This specification is basically written in three parts ...saying:

~ A set of instructions – Bytecode
~ A binary format – Class file format
~ An algorithm for identifying programs that can’t compromise the integrity of the JVM – verification.

Yes, so coming to the point. JVM is divided into 4 conceptual data spaces:

# Class Area – where the code and constants are kept
# Heap – home for object
# Java Stack – keep tracks on method invocation and data associated with that
# Native Method Stacks – Something to do with native method

Lets take an example … where is our lovely class Foo :)

Class Foo {

public static void main(String[] args)
System.out.println(“Hello World(s)”);

So tell me how many object we have?

We have 3 objects: args, out and Hello World(s). All goes into Heap.

Class Area:

Class: Foo
Method: main ( actually we have main method descriptor here)

Java Stack:

It has stack handling the push and pop of local variables like Hello World(s), out and args.

Native Method Stack: I am relaxing :)

Stack going up and down is a bogus story.

So, I am directly moving on Garbage Collector >>>

Saturday, January 06, 2007

JVM Internals - Part I

Beginning of New Year seems to be challenging for me. I am not getting enough time to recollect my ideas and write a blog on that. But in the end of last year, I decided to write blogs on JVM internals from very basic, so that a person who knows Hello World in Java can understand it. And here it goes:

Everything is sequential in this story and the most difficult is to find the entry point. Most of us are taking VM as a black box but looking inside this black box in not a tough job (neither an easy job).

VM is a machine dependent code that sacrifices itself to make java application machine independent. Lets first see the Hotspot into programmers perspective than latter we will analyze it on designer/architect perspective. Around 160 MB of this code written in C++ has 3 major parts:
~ Runtime Compilers
~ Garbage collectors
~ Everything else (JNI code, assembler code, oop (ordinary object pointer) code)

I will talk about all these later in my blog. So the folder structure goes something like this:
- vm
---- gc
---- runtime
---- compiler (communicates with other code of VM)
---- code (for Justin Time Compiler)
---- optimization
---- JNI (Java Native Interface to interact Java with c, c++ and vice-versa)
---- asm (assembler generated by VM)
---- oop (ordinary object pointers are java objects written in C++)
---- ...
---- ...

Understanding VM means understanding these parts. I will try to cover all of them in my upcoming blogs. In Sun, teams are dedicated for all these and believe me runtime people even don’t talk with gc people.