Archive for September, 2009

Debugging Java Applications with NetBeans

September 24, 2009

Netbeans provides an easy environment for debugging or troubleshooting your Java applications. With netbeans debugger, you can step through the code line by line while viewing status of variables, threads and other informations.  No need to add println() statements for finding problems that occur in your apps. Instead use breakpointss
Following things you can perform with Netbeans debugger

  1. step through application code line by line
  2. step through JDK source code.
  3. using break points, execute specific parts of code at a time
  4. track the value of a variable/expression.
  5. fix code on the fly and apply those code changes and continue the debugging session.
  6. options to suspend threads/execution at an exception.
  7. step back to the beginning of a previously called method. (pop a call in the current call stack).

Java Platform Debugger Architecture

It has 3 layers, which provides the infrastructure for debugging applications.

java platform debugger architecture

Layer-1: high-level interface for debugging

Layer-2: format of information transfer

Layer-3: low-level native interface, applies code changes at jvm level.

Debugger Parameters

Netbeans debugger allows you to enable remote debugging to already running Java application. For this, you must run your application in debug mode, which requires below parameters.

-Xdebug -Xrunjdwp:transport=dt_socket, server=y, address=<<port number>>, suspend=n

Example:

java -Xdebug -Xrunjdwp:transport=dt_socket, server=y, address=65535, suspend=n

Parameter
Description
-Xdebug enables the application to be debugged.
-Xrunjdwp loads the reference implementation of the JDWP(Java Debug Wire Protocol), which enables remote debugging
transport name of the transport to be used when debugging the application.dt_socket for a socket connection.
server y – application listens for a connection at the specified address.
n – application attempts to attach to a debugger at the specified address.
address specifies a port number used for communication b/w the debugger and application.
suspend n – application starts immediately.
y – application waits until a debugger has attached to it before executing.

Java Debug Wire Protocol(JDWP) Рwhich defines the format of information and requests transferred  between debugged application and debugger.

Attaching the Debugger to a running application.

From the main menu, select Debug -> Attach Debugger

debug-mainmenu

Below window will pop-up.

attach_debugger

Connector – select the appropriate connection type.
Transport – specifies JDPA transport protocol – automatically filled based on connector.
Host – Machine where the debugging application is running.
Port – Port number that the application listens on.
Timeout – durations that the debugger waits for a connection to be established.

Debugger commands.

Commands for debugging a java app.

F7 – step into – executes each source line , if it has method call, and source code is available, pointer moves to that method and executes it. otherwise pointer moves to the next line in the file.
F8 – step over – executes each source line without stepping through the individual instructions/commands.
F4 – run to cursor – execute the program from the current line.
F5 continue – resumes debugging until it reaches a next breakpoint or exception or until the program terminates normally.

Setting Breakpoints

Breakpoint is a marker that you can set to specify where execution should pause when you are running your application in the IDE’s debugger.
with breakpoints you can,

  • monitor the values of variables
  • take control of program execution by stepping through code line by line.
  • detect when an object is created.
  • detect when the value of a variable is changed.

Fixing code during a debugging session.

Apply Code Changes – Its an useful feature. This can save lot of time, otherwise waiting for source to be rebuilt and restarting the server/debugging session.
It is useful for

  • Fine-tune the code.(fixing minor issues)
  • change the logic within a method.

This does not work for the following changes.

  • add/remove methods or fields
  • change the access modifiers of a class, method , field
  • refactor the class hierarchy.

In simplest form, It should not accept the skeleton changes.

Note : Once restarted the server, applied code changes are not taking effect for next time because “Apply code changes” works on particular jvm instance only

For fixing code on the fly while debugging session, you must attach your application source code and jar files properly.

Advantages of using debugger

  • Easily and quickly find and resolve the problem.
  • Understand the flow of your application code is easier
Advertisements

Inspirational Book 2 : Who will cry When you die?

September 10, 2009

Had a chance to read this book, from the popular author Robin Sharma. Overall it was a nice book and I learned lots of points from the book.

Usually every person have liked some points from each book they have read. Likewise am going to share points which inspired me in this book. The book has 101 topics, among these I personally experienced that below points are good to keep/follow in your life.

Get Up Early
It would become a great day if you wake up early everyday. Sometimes back, I was woke up at 5 o clock for a month. During this period, I personally felt my mind was more clear, more active, and the power of wisdom has increased. I have been watching people who are waking up early, they are more active, have good health and have clear thoughts when comparing with people waking up late.

Master Your Time
It is a nice quote and moreover all people should practice it without saying don’t have time. Commit yourself to manage your time more effectively.
I can say if you follow the principle “Get Up Early“, surely you will manage your time perfectly and also you will get more time to think about yourself.

List your problems
A problem well stated is a problem half solved – Charles Kettering.
It was a nice quote, really I liked it.
Most of the problems disappear when you list down the problems or you can share to your best friends, after that you might be feel peaceful.

Keep Your Cool

Anyone can become angry – that’s easy. But to be angry with the right person, to the right degree, at the right time, for the right purpose, and in the right way – that’s not easy ~ Aristotle.

One of my favorite one.

In my surroundings, I have been seeing people who are getting tensed quickly. They lose their temper frequently and it becomes a habit. Oops, my friend lend this book(thanks to her) sometimes she behaves like a short tempered person. That’s why I keep saying don’t get anger for simple things unnecessarily.
Robin strategy to control the temper, really a good one.
Three Gate Test.

  1. Are these words truthful?
  2. Are these words necessary?
  3. Are these words kind?

If you pass these 3 tests, then you can deliver your words boldly.

Plant a Tree(Live a Fulfilling Life)
Three things to do, to live a fulfilling life

  1. have a son
  2. write a book
  3. plant a tree

these 3 legacies will live long after you die.
By the way, I would add one more thing in the list, having a daughter. Because I love father-daughter relationship, I am not yet into that level yet I feel it has to be nice one. Eventually Robin also had mentioned the same.

One more thing I have to say here, I have a plan to write a book titled “The Power of Observation” but don’t know when it will be published, probably you can expect it in few years.

Find 3 Great Friends
Don’t limit/resist with number 3, have the habit of making new friends. This should help to find more happiness and joy in your life.

Get Good at Asking
He who asks may be a fool for five minutes. He who doesn’t is a fool for a lifetime. ~ chinese proverb
This proverb perfectly suits my character. Till now I never hesitated to ask my needs/wants whether I get it or not. The person who asks at least has a chance of getting what he wants, the person who does not ask has no chance.

Keep a Journal
You can record your daily activities. It helps to promotes your growth and wisdom by analyzing and evaluating them.

Talk to Yourself
Keep saying your mantras(your goal, want to be like this) to yourself daily. These mantras will pave a clear path for your target, also which would help to avoid negative thoughts/wrong redirection in life. This works well for me.

Take More Risks
the more risks you take, the more rewards you will receive.
Don’t resist yourself with the same path, do things which are uncomfortable/you fear. Human minds are refusing the change, always preferred to stay in comfort zone. Be bold to come out and take more risks in your life.

Don’t pick up the phone every time it rings
I must follow this. Oh god, I want to keep this statement in mind : telephone is there for your convenience, not for the convenience of your callers.

After reading this book, I became a big fan of Robin Sharma.