How to compile and debug a hello World Program in the simplest development environment.
Applicable to cainiao programmers who do not know anything about Java syntax, or programmers who only want to write code but are not interested in object-oriented programming.
Development environment, as long as you have installed Java SDK
Source code hello. Java:
public class hello{
public static void main(String[] args)
{
System.out.println("hello world"); }
}
Compile:
> Javac hello. Java
Or
> Javac-G hello. java.
-G indicates that the generated target file contains debugging symbols (recommended because these symbols are required for the jdb debugging code later ). The compiled target file is hello. Class.
Run:
> JAVA-CP. Hello
Note that the. Class suffix does not need to be followed by Hello.
Debugging:
Enter the directory where hello. Class and hello. Java are located.
> Jdb-classpath. Hello
Enter the jdb interaction debugging environment
Set breakpoints in function main:
> Stop in hello. Main
Jdb display prompt information:
Deferring breakpoint main. Hello.
It will be set after the class is loaded.
The above hello is the class name, that is, hello in hello. Class, hello in hello. java. Main is the function name. If the "package any_namespace;" Statement exists in the first line of Hello. Java, and the correct class name is any_namespace.main, set the breakpoint statement in jdb:
> Stop in any_namespace.hello.main
Run the program after the breakpoint is set:
> RUN
We can see that jdb stops running to the position where the breakpoint is located. Set the breakpoint to the Hello class (in Java, the class name and file name are actually a meaning). The statement of line 3rd is as follows:
> Stop at Hello: 3
The statement that displays the expression is print variable_name, and the statement of the current call stack is where, the statement for one-step debugging is next or step (I don't need to worry about the difference between next and step), and the statement for the current code is list. To view the online help of jdb, enter help ).
The use command specifies the search path of the corresponding source code file for the debugging symbol. If we delete hello. java, then restart jdb, set the breakpoint, run Hello, jdb will report "source file not found: Hello. java ", enter use at this time, you can see that the current source code search path is ". ", if you set hello. copy Java to the parent directory and enter the command "use .;.. ", tell jdb not only to search for hello in the current directory. java, you also need to search for hello in the parent directory. java, and then enter the LIST Command. Now we can see that jdb correctly displays the source code.