Java debugging tips
Use conditional breakpoint
Use Exception breakpoint
How many times you have frustrated with a NullPointerException and you don't know the source from where the exception is coming. Exception breakpoints are just made for such situation. Both Eclipse and Netbeans allows you to setup Exception breakpoint. You can setup Exception breakpoint based on java exception like NullPointerException or ArrayIndexOutOfBoundException. You can setup Exception breakpoint from breakpoint window and your program will stop when you start it on debug mode and exception occurs.
How many times you have frustrated with a NullPointerException and you don't know the source from where the exception is coming. Exception breakpoints are just made for such situation. Both Eclipse and Netbeans allows you to setup Exception breakpoint. You can setup Exception breakpoint based on java exception like NullPointerException or ArrayIndexOutOfBoundException. You can setup Exception breakpoint from breakpoint window and your program will stop when you start it on debug mode and exception occurs.
Using logical structure
Logical structure option is very useful for examining contents inside java collection classes like java hasmap or Java Arraylist during java debugging. Logical view will show the contents like key and value of hashmap instead of showing full details of hashmap which we may not be interested, you can enable and disable logical view from variables window.
Logical structure option is very useful for examining contents inside java collection classes like java hasmap or Java Arraylist during java debugging. Logical view will show the contents like key and value of hashmap instead of showing full details of hashmap which we may not be interested, you can enable and disable logical view from variables window.
Step filtering
When we do Step Into on process debugging java program control goes form one class to other and it eventually go to JDK classes like System or String. Some time we just to remain in our application and don't want to navigate into JDK System classes in that case Step filtering is great you can just filter out JDK class from Step into. You can setup step filtering from preferences àJavaàDebugàStep Filtering and enable and disable it from Debug window.
When we do Step Into on process debugging java program control goes form one class to other and it eventually go to JDK classes like System or String. Some time we just to remain in our application and don't want to navigate into JDK System classes in that case Step filtering is great you can just filter out JDK class from Step into. You can setup step filtering from preferences àJavaàDebugàStep Filtering and enable and disable it from Debug window.
Copy Stack
While debugging java program if you want to copy the stack of a thread which hit the breakpoint and suspended you do so by "Copy Stack" option. Just right click on Thread on Debug Window and select "Copy Stack".
http://javarevisited.blogspot.com/2011/07/java-debugging-tutorial-example-tips.html#ixzz2WAHFgy3R
While debugging java program if you want to copy the stack of a thread which hit the breakpoint and suspended you do so by "Copy Stack" option. Just right click on Thread on Debug Window and select "Copy Stack".
http://javarevisited.blogspot.com/2011/07/java-debugging-tutorial-example-tips.html#ixzz2WAHFgy3R
About class path
I
have set my classpath environment variable as CLASSPATH=/home/test/first:/home/test/second. Now I have Test class of different version in both first and second directory.
When I give a command "java Test" What will happen ? Which Test class would be picked?
Ans:-
As JVM search directory in the order they have listed in CLASSPATH
variable So it will first go to the "first" directory
and if it finds Test.class then it take it from there and won't go to second directory
So the above trick can be used to patch releases as
So the above trick can be used to patch releases as
we used
to have a folder called "patch" listed as first element in Java CLASSPATH and any point of
time we want to put any debug statement or want to test any bug we just modify
Java source file , compile it and generate class file and put that inside patch
folder instead of creating JAR file and releasing whole new Java application. This is very handy if
you are working in a large project where you don't have development environment setup in Windows and your project only
runs on Unix
server. This approach is much faster than remote debugging Java
application in Eclipse
Here is the reason of these Java classpath errors:
ClassNotFoundException is an Exception and will be thrown when Java program dynamically tries to load a Java class at Runtime and don’t find corresponding class file on classpath. Two keyword here “dynamically” and “runtime”. Classic example of these errors is whey you try to load JDBC driver by using Class.forname(“driver name”) and greeted withjava.lang.ClassNotFoundException: com.mysql.jdbc.Driver. So this error essentially comes when Java try to load a class using forName() or by loadClass() method of ClassLoader. Key thing to note is that presence of that class on Java classpath is not checked on compile time. So even if those classes are not present on Java classpath your program will compile successfully and only fail when you try to run.
On the other hand NoClassDefFoundError is an Error and more critical than ClassNotFoundException which is an exception and recoverable. NoClassDefFoundError comes when a particular class was present in Java Classpath during compile time but not available during run-time. Classic example of this error is using log4j.jar for logging purpose and forgot to include log4j.jar on classpath in java during run-time. to read more about logging in Java see . Keyword here is, class was present at compile time but not available on run-time. This is normally occurring due to any method invocation on a particular class which is part of library and not available on classpath in Java. This is also asked as common interview questions as
“What is difference between NoClassDefFoundError and ClassNotFoundException Exception in Java” or
“When do you see NoClassDefFoundError and ClassNotFoundException Exception in Java”. By the way NoClassDefFoundError can also comes due to various other reason like static initializer failure or class not visible to Classloaders in J2EE environment. read 3 ways to resolve NoClassDefFoundError in Java for complete details.
Classpath in Java is an environment variable used by Java Virtual machine to locate or find class files in Java during class loading.
You can override value of Classpath in Java defined by environment variable CLASSPATH by providing JVM command line option –cp or –classpath while running your application.
http://javarevisited.blogspot.com/2011/01/how-classpath-work-in-java.html#ixzz2WA688B4C
Here is the reason of these Java classpath errors:
ClassNotFoundException is an Exception and will be thrown when Java program dynamically tries to load a Java class at Runtime and don’t find corresponding class file on classpath. Two keyword here “dynamically” and “runtime”. Classic example of these errors is whey you try to load JDBC driver by using Class.forname(“driver name”) and greeted withjava.lang.ClassNotFoundException: com.mysql.jdbc.Driver. So this error essentially comes when Java try to load a class using forName() or by loadClass() method of ClassLoader. Key thing to note is that presence of that class on Java classpath is not checked on compile time. So even if those classes are not present on Java classpath your program will compile successfully and only fail when you try to run.
On the other hand NoClassDefFoundError is an Error and more critical than ClassNotFoundException which is an exception and recoverable. NoClassDefFoundError comes when a particular class was present in Java Classpath during compile time but not available during run-time. Classic example of this error is using log4j.jar for logging purpose and forgot to include log4j.jar on classpath in java during run-time. to read more about logging in Java see . Keyword here is, class was present at compile time but not available on run-time. This is normally occurring due to any method invocation on a particular class which is part of library and not available on classpath in Java. This is also asked as common interview questions as
“What is difference between NoClassDefFoundError and ClassNotFoundException Exception in Java” or
“When do you see NoClassDefFoundError and ClassNotFoundException Exception in Java”. By the way NoClassDefFoundError can also comes due to various other reason like static initializer failure or class not visible to Classloaders in J2EE environment. read 3 ways to resolve NoClassDefFoundError in Java for complete details.
Classpath in Java is an environment variable used by Java Virtual machine to locate or find class files in Java during class loading.
You can override value of Classpath in Java defined by environment variable CLASSPATH by providing JVM command line option –cp or –classpath while running your application.
http://javarevisited.blogspot.com/2011/01/how-classpath-work-in-java.html#ixzz2WA688B4C
Important
Points about JVM Options:
1) Boolean
JVM options can be turned on with -XX: + and can be turned off with -XX:-.
2) Numeric
JVM Options can be set with -XX: =.
Numbers can include 'm' or 'M' for megabytes, 'k' or 'K' for kilobytes, and 'g'
or 'G' for gigabytes (for example, 32k is the same as 32768).
3) String
JVM options can be set by using -XX: =, and usually used to specify a file, a path, or a
list of commands.
No comments:
Post a Comment