Results 1 to 20 of 61
- 01-19-2009, 05:50 AM #1
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:
So there you have it... have fun.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.
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-19-2009, 05:57 AM #2
Member
- Join Date
- Jan 2009
- Posts
- 14
- Rep Power
- 0
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...
- 01-19-2009, 01:26 PM #3
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,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-19-2009, 01:41 PM #4
Member
- Join Date
- Jan 2009
- Posts
- 14
- Rep Power
- 0
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.
- 01-19-2009, 08:10 PM #5
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.
- 01-19-2009, 08:19 PM #6
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 funChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-19-2009, 09:03 PM #7
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)
- 01-19-2009, 09:42 PM #8
Sorta...
Actually it's not so much for me... it's for the forum... and this is my first stab at it.
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.Ah! I thought you wanted something that meets all three requirements.
hhhmmm... not sure I follow you on this comment. How do comments turn the debugger on/off?And turn them on/off using comments.
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.
- 01-19-2009, 09:48 PM #9
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)
- 01-19-2009, 09:55 PM #10
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,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-19-2009, 09:58 PM #11
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)
- 01-19-2009, 10:01 PM #12
"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)
- 01-19-2009, 10:22 PM #13
Great....
Excellent... that's what this all about... get the grey cells running around till they find the answer....
hahaha... No... that is not what I meant ... you'd have to comment the whole program... hehehe.Java Code:/* System.out.println("Line 15 Reached."); System.out.println("Line 16 Reached."); ... System.out.println("Impossible to Reach."); */
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-19-2009, 11:00 PM #14
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)
-
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.
- 01-19-2009, 11:50 PM #16
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 :-).
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-20-2009, 03:42 AM #17
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 01-20-2009, 12:41 PM #18
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,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-20-2009, 12:57 PM #19
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
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.
- 01-21-2009, 02:01 AM #20
I am surprised at all the answers received. Turns out that the most used non-IDE debugger is the powerful println method:
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:System.out.println("value of variable a at this point: " +a);
This is not earth breaking news... Norm has it as an excellent tip: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);}
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:
WIth the above, just need to include the debug option to enable it: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
This, along with the other suggestions in this post, should give everybody a powerful arsenal to debug java programs.java DebugExample debug
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Similar Threads
-
Java Debug Interface
By jbabe in forum Advanced JavaReplies: 2Last Post: 09-02-2008, 05:04 AM -
Not able to debug simple hello world program
By amit123solanki in forum New To JavaReplies: 1Last Post: 06-12-2008, 03:41 PM -
Java Debug Level
By keshari in forum New To JavaReplies: 1Last Post: 05-26-2008, 08:23 AM -
how to debug the servlet
By tommy in forum Java ServletReplies: 1Last Post: 08-06-2007, 08:53 PM -
how to debug java code in eclipse
By rajesh058 in forum Advanced JavaReplies: 3Last Post: 07-25-2007, 11:19 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks