Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-19-2009, 06:50 AM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
CJSLMAN is on a distinguished road
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:
Quote:
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.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 01-19-2009, 06:57 AM
Member
 
Join Date: Jan 2009
Posts: 14
Rep Power: 0
subrahmanyam.adapa is on a distinguished road
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...
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-19-2009, 02:26 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
CJSLMAN is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-19-2009, 02:41 PM
Member
 
Join Date: Jan 2009
Posts: 14
Rep Power: 0
subrahmanyam.adapa is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-19-2009, 09:10 PM
xcallmejudasx's Avatar
Senior Member
 
Join Date: Oct 2008
Location: Houston, TX & Flint, MI
Posts: 585
Rep Power: 2
xcallmejudasx is on a distinguished road
Send a message via AIM to xcallmejudasx
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-19-2009, 09:19 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
CJSLMAN is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 01-19-2009, 10:03 PM
angryboy's Avatar
Senior Member
 
Join Date: Jan 2009
Location: Javaland
Posts: 742
Rep Power: 2
angryboy is on a distinguished road
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.
__________________
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)

Last edited by angryboy; 01-19-2009 at 10:59 PM. Reason: spell err
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 01-19-2009, 10:42 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
CJSLMAN is on a distinguished road
Default Sorta...
Actually it's not so much for me... it's for the forum... and this is my first stab at it.
Quote:
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.
Quote:
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.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 01-19-2009, 10:48 PM
angryboy's Avatar
Senior Member
 
Join Date: Jan 2009
Location: Javaland
Posts: 742
Rep Power: 2
angryboy is on a distinguished road
Default
Sorry, i meant Logging class.

Quote:
hhhmmm... not sure I follow you on this comment. How do comments turn the debugger on/off?
Code:
// this requires recompiling.

// System.out.println("Line 15 Reached.");
__________________
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 01-19-2009, 10:55 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
CJSLMAN is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 01-19-2009, 10:58 PM
angryboy's Avatar
Senior Member
 
Join Date: Jan 2009
Location: Javaland
Posts: 742
Rep Power: 2
angryboy is on a distinguished road
Default
oh, it was Logger not Logging. kept mixing up the grammar. :P

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)
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 01-19-2009, 11:01 PM
angryboy's Avatar
Senior Member
 
Join Date: Jan 2009
Location: Javaland
Posts: 742
Rep Power: 2
angryboy is on a distinguished road
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)
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 01-19-2009, 11:22 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
CJSLMAN is on a distinguished road
Default Great....
Excellent... that's what this all about... get the grey cells running around till they find the answer....
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.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 01-20-2009, 12:00 AM
angryboy's Avatar
Senior Member
 
Join Date: Jan 2009
Location: Javaland
Posts: 742
Rep Power: 2
angryboy is on a distinguished road
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.
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==&& 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)
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 01-20-2009, 12:20 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,489
Rep Power: 8
Fubarable is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 01-20-2009, 12:50 AM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
CJSLMAN is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 01-20-2009, 04:42 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,513
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 01-20-2009, 01:41 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
CJSLMAN is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #19 (permalink)  
Old 01-20-2009, 01:57 PM
Senior Member
 
Join Date: Aug 2008
Posts: 374
Rep Power: 2
Supamagier is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #20 (permalink)  
Old 01-21-2009, 03:01 AM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
CJSLMAN is on a distinguished road
Default
I am surprised at all the answers received. Turns out that the most used non-IDE debugger is the powerful println method:
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:

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:
Debugging - Finding runtime problems
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:

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:
Quote:
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.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Debug Interface jbabe Advanced Java 2 09-02-2008 06:04 AM
Not able to debug simple hello world program amit123solanki New To Java 1 06-12-2008 04:41 PM
Java Debug Level keshari New To Java 1 05-26-2008 09:23 AM
how to debug the servlet tommy Java Servlet 1 08-06-2007 09:53 PM
how to debug java code in eclipse rajesh058 Advanced Java 3 07-26-2007 12:19 AM


All times are GMT +2. The time now is 06:42 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org