Page 1 of 4 123 ... LastLast
Results 1 to 20 of 61
  1. #1
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Debug challenge: how do you debug a Java program?

    Many times we have to debug our programs (when not using an IDE). So the first question here is:
    • How do you debug a Java program (without using an IDE)?

    Now that's a very simple question... the next two are a little more interesting:
    • How do you turn the debug on and off within your Java program ?
    • How do you turn the debug on and off without recomipiling the java program?


    OK... now the purpose of these questions are for those who don't know the answers and want to post they're thoughts/ideas/concepts/brain storms/light bulbs/etc.
    This idea was suggested by MC:
    MuslimCoder wrote:
    I was thinking up of something and I think I have an idea... How about say for example if some one is a begginer, just like I am. You guyz can post up a thread that asks us to make programs.
    So there you have it... have fun.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  2. #2
    Join Date
    Jan 2009
    Posts
    14
    Rep Power
    0

    Default

    Hi,
    Without using IDE to debgu
    1)you can write your java program with Accestions. When ever you want you can enable / disable them.
    2) you can use logs configured with log4j.properties. In your java program you can specify info and debug logs whenever you want you can simple configure in log4j.properties when you want to dispaly debug or info logs
    etc...

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Thanks...

    Thanks for the response ...
    1) I think you have a typo because I don't know anything in Java that's called "Accestions" (neither does Google :-)
    2) I've heard of log4j but have never tried it.

    I was sorta of thinking of a poor man's version of debuging... something you would do right on the spot to chase down a problem with your code.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    Join Date
    Jan 2009
    Posts
    14
    Rep Power
    0

    Default java assertions & log4j.properties

    Hi,
    1) Java assertions ( by mistake I typed accertions in my previous post)
    2) a) easily you can configure log4j-1.3alpha0.jar , log4j.properties file and
    b) org.apache.log4j.Logger logs = Logger.getLogger(MyJavaFile.class);
    logs.info(" this is info log");
    logs.debug(" this debug logs");

    (all the System.out.pritnln() can be used in log.info() example
    logs.info(" user name "+username); ect...)

    in log4j.properties you can specify : log4j.rootLogger=INFO
    or
    log4j.rootLogger=DEBUG


    3) for beginners we can suggest IDE itself.

  5. #5
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    Poor mans debugger.

    Lots and lots and LOTS of print statements so you can easily follow what your program is doing and where it is going. These are easy enough to comment out/remove when finished with.

    This is how I debug. I went 2 years programming before I was ever introduced to a debugger so I just got real familiar with using print statements and I think it helps improve your logical thinking patterns instead of just relying on where the debugger takes you.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default yeap..

    You're right on the mark... the poor man's debugger (quick & dirty) is the famous print/println statement.
    I'm very surprised that this post so far has offered 3 ways to debug your code (probably more to come).
    Anybody else got any other debug mechanisms (no IDE solutions please) that they would like to share?

    Thanks,
    CJSL

    PS: there are still two questions to be answered yet... have fun
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    Ah! I thought you wanted something that meets all three requirements. I would say print and assert. And turn them on/off using comments. Then there's the Logger class. Don't get this one at all. So i'm not sure if Logger meets the third requirement.
    Last edited by angryboy; 01-19-2009 at 09:59 PM. Reason: spell err
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  8. #8
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Sorta...

    Actually it's not so much for me... it's for the forum... and this is my first stab at it.
    Ah! I thought you wanted something that meets all three requirements.
    Yes and no... actually the three questions attend different needs... the first one (already answered) is somehting that you can cook up in a heartbeat and will later erase. The next two are probably for more complex programs where you want to turn the debuging on/off. The third would be probably for code that's in use by third parties and you don't want to have recompile to turn the debugger/logger on/off.
    And turn them on/off using comments.
    hhhmmm... not sure I follow you on this comment. How do comments turn the debugger on/off?
    Well... it seems that another method has been indicated: Logger class.
    4 methods suggested so far...

    Thanks,
    CJSL

    PS: so far the debug methods have only been commented... no code has be posted... gonna need that too.
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  9. #9
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    Sorry, i meant Logging class.

    hhhmmm... not sure I follow you on this comment. How do comments turn the debugger on/off?
    Java Code:
    // this requires recompiling.
    
    // System.out.println("Line 15 Reached.");
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  10. #10
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default ah....

    OK, got what you meant. That covers my first question/requirement. But are you, for example, going to comment out, say 15 - 20 debug points made of printlns? How could you turn them on/off with only one change to the code? ... not with 15 or 20 changes.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  11. #11
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    oh, it was Logger not Logging. kept mixing up the grammar. :P

    Java Code:
    /*
    System.out.println("Line 15 Reached.");
    System.out.println("Line 16 Reached.");
    ...
    System.out.println("Impossible to Reach.");
    */
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  12. #12
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    "How could you turn them on/off with only one change to the code?"

    oh ok, got your question now. hmm need to think about that one.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  13. #13
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Great....

    Excellent... that's what this all about... get the grey cells running around till they find the answer....
    Java Code:
    /*
    System.out.println("Line 15 Reached.");
    System.out.println("Line 16 Reached.");
    ...
    System.out.println("Impossible to Reach.");
    */
    hahaha... No... that is not what I meant ... you'd have to comment the whole program... hehehe.

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  14. #14
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default Assert Example

    nah I replyed, then realize what you'd meant. hehe.

    I'd never used assert before, but thought I'd give it a try.
    Java Code:
    s:\tmp>java -ea AssertExample
    Enter an integer: a
    MUST ENTER AN INTEGER.
    
    s:\tmp>java -ea AssertExample
    Enter an integer: 23
    LOGIC ERR FOUND!!
    
    s:\tmp>java AssertExample
    Enter an integer: 10
    SUM IS: 45
    PROGRAM ENDED SAFELY... GOODBYE WORLD!
    PHP Code:
      1. import java.util.Scanner;
      2. 
      3. public class AssertExample{
      4.    public static void main(String[] args){
      5. 
      6.       try{
      7.          Scanner input = new Scanner(System.in);
      8.          System.out.print("Enter an integer: ");
      9.          int n = input.nextInt();
     10. 
     11.          int i;
     12.          int sum=0;
     13.          
     14.          for(i=0;i<n;i++){
     15.             // sum++; // CORRECT
     16.              sum += i; // LOGIC ERR IN LOOP
     17.          } // end for loop.
     18.          
     19.          // desired result: i=n, sum=n
     20.          assert i==n && sum==n;
     21.          
     22.          // if no errors, then print this.
     23.          System.out.println("SUM IS: "+sum);
     24.          
     25.       }catch(AssertionError ex){
     26.          System.out.println("LOGIC ERR FOUND!!");
     27.          System.exit(0);
     28.       }catch(java.util.InputMismatchException ex){
     29.          System.out.println("MUST ENTER AN INTEGER.");
     30.          System.exit(0);
     31.       }// end try-catch
     32.       
     33.       System.out.println("PROGRAM ENDED SAFELY... GOODBYE WORLD!");
     34.       
     35.    } // end main
     36. } // end class

    what about the java debugger? does that count?
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I would recommend designing the app with an eye towards reducing errors. That would mean creating small testable classes, with drivers and test conditions. Some say to create the tests before creating the class (agile programming, I think?).

    Bottom line for it's a lot easier to debug in a small pond than in a big lake or heaven-forbid, ocean.

  16. #16
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Sure...

    AB: thanks for the assert sample... I'll take a closer look at it... Java debugger? Hell yes... that's valid. As a matter of fact, that's probably one of the best solutions.

    Fubarable: Definitely you are correct... but my idea for this post was another and was twofold:
    • Offer a change to the newbies to post code like mad on challenging topics
    • Come up with a catalog of ways to debug java code, because it seems like every other post is like "How I get 2 when it should be 0?". After all the debug options are shown and discussed, I plan to post a resume of the results (hopefully to be used by other posters... wishful thinking :-).

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    When I was start learn Java few years back, around 4 years, I workout on command prompt + notepad. Actually in early stages I don't know anything about assertions and log4j.

    So initially I use lots of println() in the code. But sometime when I wrote a long codes this gives me a mess. To cleanup those statements I've spend additional times.

    So later what I have done is, write a separate class with some overloaded methods to use as a log. Write to a text file different things, with tags like Reached Line, Result and so on. It's added real value to my application development. Later I change that to write the messages to the command prompt itself. Really easy, than opening file and see. Reason is that on command prompt I cannot use any refresh event.

  18. #18
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Thanks...

    AB: Let me back up... scratch the java debugger (jdb). It's out the scope of this post. I had confused with something else, which apparently doesn't exist (go figure). Let's just stay with inline debugging techniques.
    Eranga: Thanks for the comments... Can we see some of that debug code?

    Thanks,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  19. #19
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    I use print statements, and I throw an exception to see the stack trace ^^
    I die a little on the inside...
    Every time I get shot.

  20. #20
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    I am surprised at all the answers received. Turns out that the most used non-IDE debugger is the powerful println method:
    Java Code:
    System.out.println("value of variable a at this point: " +a);
    Now if you ever have had a program where you wanted to follow the flow of a variable through the code, you could do the following to turn the debug feature on & off:

    Java Code:
    boolean debug = true; //this is the on/off switch
    .
    .
    .
    if(debug) {println("variable a at first if: " +a);}
    .
    .
    .
    if(debug) {println("variable a at second if: " +a);}
    This is not earth breaking news... Norm has it as an excellent tip:
    http://www.java-forums.org/java-tips...-problems.html
    But now to turn on or off the debugger, you have to recompile the program. While not a show stopper, it can be tiresome. Taking this a step further, what if we could turn it on or off at the command prompt:

    Java Code:
    import java.util.*; 
    public class DebugExample
    {
    public static void main(String[] args) 
    {
     boolean debug = false;
     if (args.length > 0 && args[0].toLowerCase().equals("debug"))
        {
         debug = true;
        }
    
     if (debug)
     {
      System.out.println("debug option was selected");
     }
     else
     {
      System.out.println("Nope... debug option was NOT selected");
     }
    	
    }//end main 
    
    }//end DebugExample
    WIth the above, just need to include the debug option to enable it:
    java DebugExample debug
    This, along with the other suggestions in this post, should give everybody a powerful arsenal to debug java programs.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Page 1 of 4 123 ... LastLast

Similar Threads

  1. Java Debug Interface
    By jbabe in forum Advanced Java
    Replies: 2
    Last Post: 09-02-2008, 05:04 AM
  2. Not able to debug simple hello world program
    By amit123solanki in forum New To Java
    Replies: 1
    Last Post: 06-12-2008, 03:41 PM
  3. Java Debug Level
    By keshari in forum New To Java
    Replies: 1
    Last Post: 05-26-2008, 08:23 AM
  4. how to debug the servlet
    By tommy in forum Java Servlet
    Replies: 1
    Last Post: 08-06-2007, 08:53 PM
  5. how to debug java code in eclipse
    By rajesh058 in forum Advanced Java
    Replies: 3
    Last Post: 07-25-2007, 11:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •