11.6 Debugging Tips 1) A less well-known but very effective technique is to put a main method in each class so that each class can be unit tested. This method can be preserved because the Java Virtual machine only calls the main method of the startup class. 2) Log agent, (logging proxy), through a subclass object, stealing the parent class's method call, and logging in it, the sample code is as follows:
Random random = new Random{
public double nextDouble(){
double result = nextDouble();
Logger.getGlobal().info("nextDouble:" + result);
return result;
}
}
3) Use Throwable's Printstacktrace method to print the call stack, but it is not necessary to print the stack information by exception, as long as you can print the current call stack information by calling Thread.dumpstack () anywhere in the code. Such information is generally displayed in the System.err.
4) How to capture the output stream and error stream in a file
捕获输出流
java MyProg >log.txt
捕获错误流
java MyProg 2>log.txt
同时捕获输出流和错误流
java MyProg >&log.txt
5) The processor that can change non-default catch exception
Thread.setDefaultUncaughtExceptionHandler{
new Thread.UncaughtExceptionHandler(){
public void uncaughtException(Thread t, Throwable e){
//save info to file
}
}
}
6) If you want to see the class loading process, you can invoke the-verbose flag to start the Java Virtual machine, so you can see some output similar to the following. This method is useful for diagnosing problems that arise as a result of classpath.
7) Use Java-xlint:xxxx to tell the compiler to do some grammar checking.
8) There is a jconsole program that can be used to monitor the various parameters of a Java Virtual machine jconsole
virtual machine Process ID
9) You can use the Jmap utility to capture a heap dump that shows each object in the heap, using the following command: Jmap-dump:format=b, File=dumpfilename processId
Jhat Dumpfilename
You can then use your browser to enter http://localhost:7000 to view the objects in the pair in the dump pair.
10) If you run a Java virtual machine using the JAVA-XPROF flag, you run a basic profiler to analyze the methods that are often called in your code. The profiling information is sent to the System.out output.
11.8 Using the debuggerThis is a basic meeting, nothing to look at.
From for notes (Wiz)
Java series: Java Core Technology Vol. 1 Learning notes, Chapter 11 Debugging Tips