*
The moose likes Beginning Java and the fly likes how to debug Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to debug" Watch "how to debug" New topic
Author

how to debug

Karen Nelson
Ranch Hand

Joined: Aug 19, 2005
Posts: 57
Hello,
I am new to java but have been programming for a while in other languages. Usually when debugging I make heavy use of a MsgBox (from Visual Basic) or something like that -- a way to send myself messages as the code is running. Is there anything like this is java?
Thanks! Karen.
Rick Beaver
Ranch Hand

Joined: Dec 14, 2004
Posts: 464
For simple debugging you can just use some System.out.println statements at relevant places in your.

If you need more detail about the state of your application many IDE's have powerful debuggers which enable step by step analysis of all aspects of your application as it is running - personally I use the Eclipse debugger heavily.


ph34r my 133t j4v4 h4><0r1ng sk177z
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi Karen,

Welcome to JavaRanch!

Well, there's System.out.println(), and javax.swing.JOptionPane, and there are several nice logging APIs, but none of this really qualifies as "debugging". There's a command-line debugger JDB for that, and there are a number of excellent graphical debuggers -- most every Java IDE has one -- that let you step through source, set breakpoints, inspect variables, etc.


[Jess in Action][AskingGoodQuestions]
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
Test-driven development is an excellent alternative to debugging with print statements. Agile Java (Langr) is a book (none finer) that covers that in detail and will put you on the right path for Java development.

Meanwhile, try using assert statements to "debug" your code. They are very simple to use and very effective. You can use them to output values in context, just like MsgBox:



When you run java you have to enable asserts: java -ea ....

Easy, effective, and definitely conforms to best practices for debugging. IDE debugging (setting breakpoints and such, as you could do in VB) is also indespensible at times.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Here is an article I wrote for the Journal.

The key thing to know about debugging is the -Xdebug and similar command arguments that can be passed to the java command.

Now, the article is setting up remote debugging, where your IDE is running in one JVM and the code you want to debug is running in a totally different JVM outside the IDE.

Like said before, for local debugging most IDEs have that built in, just like runing your app through the IDE, it is the same.

Good Luck.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
>> The key thing to know about debugging is the -Xdebug and similar command arguments that can be passed to the java command.

Hmmm... Looks like an article on setting up remote debugging with an IDE. Use that all the time with Eclipse myself, however, it's far from the key thing to know.

Integrated instrumentation in the form of logged "MsgBox" output is key. Add to that assertions and then, as a very useful piece of fluffery, IDE debugging.
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
I guess most of you are lucky to have IDEs and are able to do "interactive" debugging. I don't have any fancy tools and am stuck with println()s and have been using them for years. I think it qualifies as "debugging".
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
I think that you have a lot of programmers who wait for bugs to be reported then fix them in the IDE. That is to say, they do not use defensive coding first and foremost: assert, unit test. They also don't do a lot of logging even though it's a frequent requirement on a deliverables list.

I just love the IDE for fixing bugs that were not caught in tests or are not obvious in the logs. Wonderful tool, but not the key to debugging the code.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
We seem to be talking about a couple of different kinds of debugging here. System.out.println is great when the code doesn't run and you can't figure out what's going wrong with it. Even with the IDE debugger, it is useful to narrow down where the problem is so you can see where to put a breakpoint. I use it a lot sometimes.

However, when the code is running and you're coming up with the wrong output, I would have to agree with Rick; assert, unit test (JUnit, etc), and log (log4j is pretty good in my experience) to prevent/fix bugs.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Karen Nelson
Ranch Hand

Joined: Aug 19, 2005
Posts: 57
Hi, this is Karen again. Thanks for all the replies! I had forgotten that I can use the command line and Swing at the same time. I don't use an IDE (yet anyway) so that's out. And usually I'm the end user of my programs, so I haven't gotten into more serious debugging much. Lots to learn!
-Karen.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

Test-driven development is an excellent alternative to debugging with print statements.

Buy this man a beer. I cherish intelligent remarks on internet forums - such a rarity. Makes me want to put it in my pocket for show and tell.


Tony Morris
Java Q&A (FAQ, Trivia)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to debug
 
Similar Threads
hello everyone
Java 5 - books (not necessarily certification ones)
instant messaging drivel
Should we learn hacking?
What is your first language ?