Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to debug java code?

 
Amisha Shah
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can i debug java code step by step ? using command line and also using jcreator.
 
Michael Lachenmaier
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48645
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Rancher
    Posts: 42967
    73
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    There's also an excellent debugger that's not tied to an IDE - JSwat.
     
    Henry Wong
    author
    Marshal
    Pie
    Posts: 20992
    76
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    Don't get me started about those stupid light bulbs.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic