It's not a secret anymore!*
The moose likes Beginning Java and the fly likes how to debug java code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to debug java code?" Watch "how to debug java code?" New topic
Author

how to debug java code?

Amisha Shah
Ranch Hand

Joined: Mar 03, 2006
Posts: 33
how can i debug java code step by step ? using command line and also using jcreator.


Amisha Shah.<br />SCJP 1.4
Michael Lachenmaier
Greenhorn

Joined: Jan 15, 2007
Posts: 2
Sorry, I don't know jCreator. But I use NetBeans IDE and it has comfortable debugging features, and .... IT'S FREE!!! So, try out http://www.netbeans.org/
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
one way of doing it is just "System.out.println();"
you just print certain variables at different steps in the program
and you can see whats happening in that certain
method or whatever. I know many people dissagree that it's
useful, especially when you have netbeans debugger, but thats
what i do.

Justin Fox


You down with OOP? Yeah you know me!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

From the command line: The Java SDK includes a tool called jdb, the Java debugger. Read the documentation here. However, this tool isn't very user-friendly and I wouldn't recommend it unless there's no alternative you can use.

IDEs such as NetBeans (mentioned above) and Eclipse have very good and user-friendly debuggers which allow you to set breakpoints, step through the code, look at the values of variables etc. I've never used JCreator but probably it also has a debugger like that. Have a look at the JCreator documentation.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38044
    
  22
In JCreator you only have a debugger on the posh paid-for version, which you can try free of charge for a month.
If you have the LE version like me, you will have to follow Justin Fox's suggestion.
Look at this method which is supposed to add the sum of successive numbers (those who know a little arithmetic will remember this is not an efficient method to do this, and the more eagle-eyed Ranch members will see the reason for the out-by-one error before trying the method);Now, if you set it up with 0, 10, your printout reads 0 1 2 3 4 5 6 7 8 9. At this point you can see how the out-by-one error occurs. The //test bit allows you to use ctrl-F to locate all such testing code and delete it (or, for an assessment, to comment it out and show what you were up to).
The JOptionPane in the constructor is a modal dialogue; it stops processing until you click its "OK" button, so you can halt processing wherever you wish.

So, using these display methods allows you
  • to follow the flow of code by showing line numbers or method names,
  • to allow methods to run freely, if you don't put any messages in them,
  • and (probably most important of all), to see the values of variables and objects during the flow of processing.

  • I hope this is of some help.

    CR
    Ulf Dittmer
    Marshal

    Joined: Mar 22, 2005
    Posts: 41133
        
      45
    There's also an excellent debugger that's not tied to an IDE - JSwat.


    Ping & DNS - my free Android networking tools app
    Henry Wong
    author
    Sheriff

    Joined: Sep 28, 2004
    Posts: 18543
        
      40

    Originally posted by Justin Fox:
    one way of doing it is just "System.out.println();"
    you just print certain variables at different steps in the program
    and you can see whats happening in that certain
    method or whatever. I know many people dissagree that it's
    useful, especially when you have netbeans debugger, but thats
    what i do.


    Don't know about other people, but I'll add an "agreement" to this.

    Maybe it is because I generally work with Grids (lots of networked computers working as an unit), but I also either, do a lot of system outs, or bumping up the log4j levels, for the classes in question, on the nodes in question.

    The eclipse debugger is great for the early stages, when I'm debugging certain (isolated) features, on a single node, which is running on my computer. However, this case doesn't occur very often.

    Henry


    Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: how to debug java code?
     
    Similar Threads
    Debugging projects with AspectJ ?
    debug
    Null Pointer Exception error
    exception thrown from the servlet service function
    Debugging in java