Create Java Project
When Eclipse first starts to enter the resource perspective, in this perspective can manage projects, folders, files and other resources, according to Eclipse's claims, the panel called the view, the view is called a complete perspective, inresource perspective, you can use the top left of the view (Map View) Navigation and the creation of resources.
In Eclipse, before you do something, such as creating Java programs, create a Java project, create a Java project as follows:
1 Right-click on the navigation view in the pop-up context menu, select New Project;
2, in the New Project dialog box, Eclipse provides the project options: Java, plug-in development.So you want to create a Java project, in the dialog box on the left select Java;
3, the right side of the dialog box, select the Java project, if you install the Java plug-in development of other packages listed here will be together (such as EJB, Servlet, etc.), Eclipse JDT installed by default only supports the standard Java application, so youJava Project option must be selected;
4, click Next to enter the New Project Wizard dialog box;
5, the first mission to the project a name, because it is a simple "Hello, world" example, so here I enter "Hello", click Next, set the options for building Java projects set for this project you do not need to do anythingunmodified;
6 Click Finish;
7, Eclipse will prompt you at this time and this project is associated Java perspective, and ask if you switch to the Java perspective, select Yes.
Create Java classes
When you create a good Java program created after the Java program can begin, though not have to do, but it can organize your Java classes into the package, this is a good practice, often using the domain name as the package name,This reduces the possibility of name clashes, if you do not register a domain name, you can use any convenient, the only hoc name, I used here org.eclipseguide, the project name is hello, so the full name of the package is org.eclipseguide. hello.
Follow these steps to create a Java program:
1, right-click on the project, select New Java Class Wizard New.Class pop;
2, the first field area "source folder" Default is the project folder, keep the default value;
3, the regional input in the package field org.eclipseguide.hello;
4 regional input name field in the class HelloWorld;
5, in the "Which Method Stubs Would You Like to Create?" Area, select public static void main (String [] args), the New Java Class dialog box shown in Figure 1.
Figure - 1 using the New Java Class Wizard to create HelloWorld class
6, click Finish, the new java package class wizard will create a series of directories, HelloWorld.java source file will be placed in the package name below.
Automatically create the code includes a main () method, which you can add any features, such as printing your Hello, world!, To make debugging more interesting, you need to add a loop printing method, in Eclipse, automatically changesgenerated code, as follows:
public class HelloWorld {
public static void main (String [] args) {say ("Hello, world !");} public static void say (String msg) {for (int i = 0; i <3; i + +) {System.out.println (msg );}}}
Java program running
Now you have to run the Java program, and run Java programs need to consider a number of things, including Java runtime environment, the need for command line arguments, if I do not know a class has a main () function whether the use of which, in Eclipse,the correct way to start a Java program from the Eclipse menu, select "Run? run", will pop up a dialog box that allows you to configure the boot options, running a Java program, you'll need to create a launch configuration or from the currentwith the configuration chosen.
For most simple terms of Java programs, you do not need to specify the boot configuration, so you can use a simpler method to start programs, first make sure that the editor all the HelloWorld source code is selected, then:
1, from the Eclipse menu, select Run? To ... run? Java application;
2, because you have made changes to the Java program, Eclipse will prompt you to save before running Java programs look, click OK;
3, the task view to switch to the console view, to display program output shown in Figure 2.
Figure - 2 Eclipse console view shows the HelloWorld run output
You may wonder why there is no separate steps. Java file into. Class files, this is because Eclipse JDT includes an incremental compiler to evaluate the Java code you entered, it can highlight syntax errors and incompletereference, and other Eclipse-friendly features, like, if you think this feature is useless to you, you can turn it off if compiled successfully,. class files are synchronized when you save the source file saved.
Debugging Java programs
Interactively run the code in the Eclipse is one of the most powerful features, the use of JDT debugger, you can execute your Java program line by line, checking the value of program variables at different positions, a process that the problems in positioning the code is very useful when.
To prepare for debugging, you need to set a breakpoint in the code first, so that the debugger allows you to debug suspend, otherwise, the program will execute from the beginning to the end, you have no chance to debug.To set a breakpoint in the editor on the left edge of the gray double-click here to say () function set the breakpoint position, then will display a blue dot, indicating an active breakpoint.
Run the program under the debugger and run it are very similar, Eclipse provides two options: select "Use full-service operation? Debug" menu with a quick configuration, if the default option correctly, you can choose "Run? To... debug? Java applications. "Here we use the latter.
To ensure that the code editor have all been selected HelloWorld, and then from the main menu, select "Run? To ... debug? Java applications", Eclipse will start the program, switch to the debug perspective, suspended at the breakpointshown in Figure 3.
Figure - 3 debug HelloWorld: In the first breakpoint execution is suspended
Debug perspective includes a number of new views, are used for debugging, first of all, in the upper left is the debug view (not to be confused debugging perspective), it shows all the call stack and the current status of all threads, includingAll the threads have been implemented, the program runs to the breakpoint location, the status is displayed as suspended.
Single-step debugging code
Debug view title bar is a Java program allows you to control the implementation of the toolbar, in front of a few buttons and electronic equipment, such as CD player control buttons are very similar style, allowing you to pause, continue and terminate the program, these buttons let youcan execute code line by step, each button on the mouse button when the message will be displayed, such as tracking, single step, to return and so on.
If the second button tracking code currently executing is highlighted in the editor status: call say () function.If the track they would have access to this function inside, click on the track, the highlight of the trip is the first implementation of the line - for statement.
In general, you want to write your own class track for standard class and third-party classes are usually no problem, do not need to track, this time after the tracking filter can be used, that is Step With Filters, select Window.Preferences. Java.Debug.Step filter, and then select the list of packages and classes, more time to set the filter can effectively reduce the difficulty of debugging, when you use the normal trace debug functions often encounter many unknown error, thenTracking will be able to use the filter to solve this problem very well.
Assessment of variables and expressions
Debug view is the right view window contains a label, where you can examine and modify variables and breakpoints, select the variables tab, this view shows the current range of variables and their values, before entering the for loop, this view includes onlysay () function msg parameters and their values, single step or trace button to enter the for loop, variable loop symbol view will show the value of i and 0.
Sometimes a Java program has many variables, but you are interested in only one or a few, in order to monitor the selection of variables and expressions, you can add them to the expression of views in the watch list, for example, select a variable i(double-click it in the editor), right-click, right from the pop-up menu, select Monitor, the variable (its value) will appear in the expressions view.
One advantage of monitoring variable objects and you can check details of their values, and can modify their values.Back to the variables view, expand the msg variable to display its properties, it is a char array, expand the msg string to display its value, for example, double-H, will prompt you to enter a new value, such as J.
In the same label is displayed in the view window, which allows you to enter any permitted variable or expression, including examples of these variables, select the display view and enter:
msg.charAt (i)
In order to quickly evaluate the expression, you must first select it, then click the Tools button on the second display view (Display the selected text results of the assessment), it displays the results in the display view, is usually best to click on the first tool button (checkassessment of the selected text), because it adds expression to the expression of views, regardless of which method to use, when the expression of the variable changes, the value displayed is not back to the automatic update.However, in the expression view, you can speak into the watch expression expression, when you step through the code will automatically update, switch to the expression of views, attention to check the button to display the next expression, the expression and then clickpop-up context menu select Convert to monitor expression, the icon will become the next expression to monitor icon.
We go back to step through the code, before you call System.out.println () when the left cursor, if you want to view System.out.println () of the code, you can click on the track, or click on a singleStep System.out.println () function to start the next for loop.
Below the editor window, with a label, including the console view, the program output is displayed here, if you will, as said earlier H to J, and that will be shown here, Jello, world!.You can also click the step, until the loop terminates, if you think this process is too long, you can click back a quick end to say () function to return to the main () function, or click the Continue button allows Java programs to run to the finish.
Summary
Eclipse of the most useful features is its integrated debugger, which can interactively execute code by setting breakpoints, execute code line by line, checking the values of variables and expressions such as functional response of its powerful features, it is aCheck and repair the problem Java code irreplaceable tool.