Eclipse Getting Started Tutorial

Eclipse Getting Started Tutorial When it 
comes to the Java IDE, the most catchy one is Eclipse. If you can be proficient in Eclipse, it will have a multiplier effect for us to write java programs, and greatly improve our work efficiency. Therefore, in this blog post, the author is only for newcomers who have just started java, so that they can master the use of Eclipse as soon as possible. 
1. Common shortcut keys 
This is the first step in using tools. Skilled use of shortcut keys will help us a lot in writing programs. Therefore, the shortcut keys listed by the author here suggest that everyone must master them. 
Ctrl + left mouse button (variable nouns of classes, methods, properties): locate and track the position of a variable declaration or definition 
Ctrl + S: save the current file 
Ctrl + X: cut 
Ctrl + C: copy 
Ctrl + V: paste 
Ctrl + D: Delete current line 
Ctrl+F: Find/Replace (current editing window) 
Ctrl+H: Global search 
Ctrl+/: Comment current line or lines of code 
Ctrl+Shift+C: Comment current line or lines of code 
Ctrl+Shift + F: Format the current code 
Ctrl + Shift + O: The missing Import statements are added, and the redundant Import statements are deleted (first position the cursor on the class name of the package to be imported) 
Ctrl + Shift + S: Save all files 
Ctrl + Shift + X: Change all the currently selected text to uppercase 
Ctrl + Shift + Y: Change all the currently selected text to lowercase 
Alt + /: Code intelligent hint 
Alt + Shift + R: Rename (including file name, class name, method name, variable name, etc., very useful) 
Alt + Shift + J: Generate class or method comments 
Alt + Shift + S: Open Source window ( Generate get, set methods, implement, override interface or class methods, very common) 
Alt + Shift + D, J: If there is a main method entry, execute the code in Debug mode 
Alt + Shift + X, J: If there is a main method entry, execute the code in Run mode

2 Common concepts and operations 
2.1 Project (Project) 
All the resources that can be compiled and run in Eclipse must be placed in the project, and many functions are not available when opening the file alone. A project represents a set of related files and settings (such as settings for classpath, compiler level, release path, etc.). Generally speaking, the two files .project and .classpath in the directory describe the information of the current project. To open a project, you can first select a single or multiple projects, and then select the menu Project -> Open Project, or right-click and select the menu Open Project. To close a project, you can first select a single or multiple projects to be closed, and then select the menu Project -> Close Project, or right-click and select the menu Close Project. 
2.2 Workspace (Workspace) 
An Eclipse can have multiple workspaces, each workspace contains multiple projects, as well as all the rest of the setting information such as interface layout, text size, server definitions and so on. But a workspace can only be used by a single Eclipse process. In addition, the same project will be added to different workspaces. Note that when deleting the workspace directory, it is possible to delete the project files located in the workspace by mistake. The workspace directory will have a directory called .metadata to hold all settings information. When Eclipse starts it will let you choose which workspace to use. If the entered workspace directory does not exist, Eclipse will automatically create it. 
2.3 Import and export Java projects 
2.3.1 Import projects 
After downloading the source code file containing the Eclipse project, we can import it into the current Eclipse workspace for editing and viewing. Click the menu File->Import, then expand the General directory in the pop-up Import dialog box, select Existing Projects into Workspace, and then click the Next button. When the radio button Select root directory: is selected, you can click the Browse... button to select the folder containing the project. If it contains a project, it can be displayed in the Projects list box in the middle; and when the radio button Select archive file: is selected, you can click The Browse... button selects the ZIP archive containing the project, and if it contains the project, it can be displayed in the Projects list box in the middle. Finally, click the Finish button to import the project and open it. As shown in the following figure: 
write picture description here 
Figure 3.11 Import project 
2.3.2 Export project 
Click the menu File->Export, then expand the General directory in the pop-up Export dialog box, select Archive File, and then click the Next button. Then select the file name to be saved in the To archive file: output box, usually written as project name .zip, and then click the Finish button to export the current project. Another way is to package manually, you can use WinRAR or WinZIP and other tools, first click on the toolbar to open the directory where the project is located, and then you can use your favorite tool to package the code directory. 
2.4 Quickly fix code errors 
After writing code in the Eclipse editor and compiling, the checked errors or warnings will be displayed and a red light bulb will be displayed on the bar at the beginning of the problematic code line. Left-click on the light bulb or press the shortcut Ctrl+1 (or menu Edit > Quick Fix) to display correction comments and show a preview before the correction. As shown in the following figure: 
write picture description here 
Figure 3.12 Quickly fix code errors 
2.5 Optimize the import list 
Useless packages and classes are often imported in the code, which can be reorganized through the menu Source -> Organize Imports or by selecting the menu item Source -> Organize Imports in the context menu of the editor, or pressing the shortcut key Ctrl+Shift+O. And get rid of useless classes and packages. 
2.6 Adding, Modifying, Deleting JREs 
From the menu Window -> Preferences, and then selecting Java > Installed JREs, you can open the list of JREs for writing programs in Eclipse. The JRE checked by the checkbox is the default JRE, which is used by all projects in the project as the JRE for compilation and startup (unless another JRE is specified in the project's Build Path). You can add a new JRE definition through the Add... button (select the Browse... button in the pop-up dialog box, then select the JDK installation directory, and then click OK), Edit... button to modify the JRE definition, and Remove button to delete the JRE definition , check the box in front of a different JRE to make it the default JRE. Although MyEclipse can automatically find and display a JRE, it is strongly recommended that you add a JDK for development, so that you can view the JDK class source code and display prompt information when coding. As shown in the following figure: 
write picture description here 
Figure 3.13 Configure the installed JRE 
2.7 View the class definition, hierarchy and source code 
View the class definition or its source code, you can select Open Declaration in the context menu of the editor, or select the menu Navigate -> Open Declaration, or press F3 key. If this class is associated with the source code (such as the class in the JDK), you can see the source code, otherwise you can only see the method and member information of the class. 
To view the inheritance hierarchy of the class, you can select Open Type Hierarchy in the context menu of the editor, or select the menu Navigate -> Open Type Hierarchy, or press the F4 key, or drag and drop the class or package into the Hierarchy view, you can view the Hierarchy The view sees the inheritance hierarchy of the class, and then you can click on the corresponding class to see the definition. 
2.8 Find class files (Open Type) 
To quickly find the definition of a type, select the menu Navigate -> Open Type, or press Ctrl+Shift+T, or press the toolbar button. At this time, the Open Type dialog box can appear, type the first few letters of the class in the Enter type name prefix or pattern input box, or you can use wildcards such as ? and * to fuzzy search, the list below the dialog box will display Matching class files, select the single or multiple class definitions shown in the list to open it. If this class is associated with the source code (such as the class in the JDK), you can see the source code, otherwise you can only see the method and member information of the class. As shown in the following figure: 
write picture description here 
Figure 3.14 Open Type dialog box 
2.9 Source directory, output path, Library and compiler version settings 
Click the menu Project -> Properties or right-click on the Package Explorer project node and select Properties in the context menu, or use shortcut keys Alt+Enter to open the project properties dialog. Select the Java Build Path on the left to display the project's classpath-related settings tab on the right. The Source page shows the source code directory (one or more can be used, and the source files in it will be compiled) and the directory where the class files generated after the Java source code is compiled are stored. These parameters can be modified, and source code directories can be added or deleted. The Package Explorer view does not display the output directory for class files by default. As shown in the following figure: 
write picture description here 
Figure 3.15 Source directory and output path 
Click on the Libraries page to set the classpath of the current project, these class libraries are used when compiling source files. As shown in the figure below: 
write picture description here 
Figure 3.16 Class Library 
Add JARs button can add jar files in the current project to the classpath, Add External JARs will add jar files outside the project to the classpath, Add Variable adds variables, and Add Library can add classes Library (a collection of one or more jar files, defined and managed by development tools), Add Class Folder adds class files in the directory, Edit can modify the settings of the selected class library, and Remove removes the selected class from the classpath library. 
In the development, it is inevitable to set the compilation level of the source code. For example, if JDK1.6 is used to develop a project that will run on JDK1.4 in the future, then the compiler level needs to be set at this time, otherwise the future class files will be changed due to the over version. high and not recognized by the target JDK. Click Java Compiler in the project properties dialog to set the compiler level of the code. As shown in the following figure: 
write picture description here 
Figure 3.17 Compiler level 
If you only modify the compiler level of the current project, you can select the checkbox Enable project specific settings, and then select the target compilation level in the drop-down box to the right of Compiler compliance level, such as 5.0, 1.4 and so on. You can also uncheck the Use default compliance settings checkbox for further settings. These settings will affect the syntax error checking in the source code. For example, if you want to write code with 5.0 syntax in a 1.4-level project, you will definitely get an error. 
If you want to modify the default compilation level of all projects, click Configure Workspace Settings... to open the global settings dialog box. The settings in these two places are almost the same, so I won't repeat them. 
2.10 Generate getter and setter methods 
When writing JavaBeans, it is often necessary to write some modeled methods such as getXXX() and setXXX(). We can use Eclipse to automatically generate these modeled methods. Write a variable definition like private String name; first, then select the menu Source -> Generate Getters and Setters... or right-click in the editor and select the menu Source -> Generate Getters and Setters... to open the Generate Getters and Setters dialog box, In the dialog box, select the method to be generated, and then click the OK button. 
2.11 Formatting source code 
Sometimes the code is handwritten in a mess. At this time, you can first select the code to be formatted (do not select to format all the code in the current file), select the menu Source -> Format or right-click in the editor Select the menu Source -> Format or use the shortcut Ctrl+Shift+F to quickly format the code into a readable format. This operation can also format XML, JSP, HTML and other source files in MyEclipse. 
Comment and Uncomment 
Use the shortcut key Ctrl + / to quickly add or remove two slash (//) style comments to the selected code. 
2.12 Manual and automatic compilation 
If it is a very large project, such as thousands of source code, using Eclipse to automatically compile will be a nightmare. Every time you type a line of code, the compiler check process is automatically started, and the screen even freezes in severe cases (this is also a disadvantage of Eclipse's advantages). At this time, you can switch Eclipse's automatic compilation to manual compilation. After removing the selected state of the menu Project -> Build Automatically, the project becomes a manual compilation state; click the menu again to switch to the automatic compilation state. At this time, typing code again will not automatically check for compilation errors, and will not generate compiled class files, which helps to write code quickly. To compile at this point, you can select the menu Project -> Build Project to compile the current project or Project -> Build All to compile all projects. 
2.13 Paste the Java source code directly as a class file 
Eclipse 3.3 supports a function that if the Java source program is placed on the clipboard, for example, the following code is copied to the clipboard:

public class YetAnother { 

Then click the menu Edit -> Paste or select Paste in the context menu of the project node of the Package Explorer view, or press the shortcut key Ctrl + V, then Eclipse will automatically generate a new .java based on this code file and add it to the current project's source code directory. 
First select the file node of the Package Explorer view (Java class or ordinary file can be), then click the menu Edit -> Copy or select Copy in the context menu of the project node of the Package Explorer view, or press the shortcut key Ctrl + C, then If you select Paste again, a copy of the original file will be created at the location to be pasted, and if it is a class, its package definition will be automatically modified or you will be prompted to enter a new name for the class. If you select a file or folder in the Windows file browser and copy it and then paste it in Eclipse, the file or folder will be immediately copied and added to the current project, allowing you to quickly import some individual source code . 
2.14 Breakpoints and debuggers  Double-clicking the mouse on the spacer of the source code can
switch whether to set a breakpoint in the current line 
write picture description here 

Run->Debug, or Run -> Debug As -> 1 Java Application, or through the toolbar button, or the shortcut key F11, or select Debug As -> 1 Java Application in the context menu of the editor to start the debugger. When the debugger hits a breakpoint, it suspends the current thread and switches to the debug perspective. The Debug perspective will display the Debug view, Variables view, Breakpoints view and Expressions view. For example, when our program is debugged, it looks like this: 
write picture description here 
Figure 3.19 Debug view 
The Debug view shows all currently running threads and the location of the code being executed. 
At this time, the editor will use a green highlighted line background to indicate the location of the executed code, as shown in the following figure: 
write picture description here 
Figure 3.20 Code indicator during debugging 
and the Variables view displays the values ​​of local, global and other variables in the current method or class . 
write picture description here 
At this time, the thread has been suspended. Click the Resume button in the Debug view to continue execution. To resuspend, you can select a thread and click the Suspend button. To debug code line by line, you can click the Step Over button to execute down, or press the F6 key. To stop debugging, you can click the button. 
Note: The debugger in Eclipse is very functional, but it is very complicated to use. For more detailed information, please refer to IBM's developer site or Eclipse's help documentation. Due to space limitations, no further introduction will be given here. 
2.15 Quickly add and delete jar packages to Build Path 
First, copy the jar file to the project (refer to the section on copying files in the project), then right-click the jar file in the Package Explorer view and select the menu Build Path -> Add to Build Path can add the jar file to the Build Path; to remove the jar file from the Build Path of the project, you can select the menu Build Path -> Remove from Build Path. 
If it is a MyEclipse Web project, when you add the jar file to WebRoot/WEB-INF/lib, MyEclipse will automatically add it to the current project. If you find that the newly added file is not displayed in Eclipse, you can select Refresh in the context menu in the Package Explorer view or press the shortcut key F5 to see it. 
2.16 Check which classes the current class is referenced by 
In the project, if you can see the classes or variables, and which other classes the methods are referenced by, it will greatly speed up the progress of debugging or understanding the program structure. You can select References -> Project in the context menu of the editor to display which classes in the current project refer to it, or References -> Workspace to see which classes in the entire workspace refer to it. The search results are displayed in the Search view. 
2.17 Set editor font, color and display line number 
By default, Eclipse's code editor does not display line numbers. To display it, you can open the Preferences settings dialog through the menu Window -> Preferences..., almost all Eclipse settings options All can be found here. To display line numbers, expand the node General -> Editors -> Text Editors, and select the checkbox Show line numbers in the settings on the right. As shown in the figure: 
write picture description here 
Figure 3.22 
The editor showing the line number is shown as follows: 
write picture description here 
Figure 3.23 The editor showing the line number 
To modify the font of the editor, you can select General -> Appearance -> Colors in the Preferences dialog box and Fonts, then you can modify the font on the right side. Note that the editor's font is set to Basic->Text Font, and then click the Change... button. As shown below: 
write picture description here 
2.18 Link file 
Eclipse supports a special concept called Link file, which is very similar to the Windows shortcut concept. Select the menu File -> New -> File or File -> New -> Folder to open the dialog box for creating a new file or directory, as shown in the following figure: 
write picture description here 
Figure 3.25 Link method to create a file 
At this time, if you click the Advanced button, and then select the check box Link to file in the file system, then you can click the Browse… button to select other files outside the project. The file created at this time is called a Link file, which is equivalent to a shortcut. The real content is stored in c:\HelloWorld.java, but the modification of this file in the project will be automatically synchronized to c:\HelloWorld.java as if the file was in the current project. The icon of the created file will show an arrow to indicate that the file is a Link file, which looks like this; the icon of the directory will look like this: . The Java source code in the Link directory can also be added to the source code directory for compilation. 
Note: Because the files in the Link method depend on the absolute path of the file system, it is not recommended to use this method to package your project and send it to others for use. 
2.19 Installing plug-ins 
General Eclipse plug-ins only need to be copied to C:\Java\MyEclipse6.0\eclipse\plugins and the installation can be completed. Such plug-ins are generally separate jar files, we assume C:\Java\MyEclipse6. 0 is your MyEclipse installation directory. If you find that the downloaded plug-in is in ZIP format and there is an eclipse subdirectory after decompression, you need to copy it directly to C:\Java\MyEclipse6.0 to overwrite the eclipse directory to complete the installation (be careful not to delete the old eclipse directory) . 
2.20 Get help and read help documentation 
Press the F1 key anywhere, and Eclipse will display the relevant help documentation; the complete help documentation can be read through the menu Help -> Help Contents. Most of the operation instructions of MyEclipse and Eclipse, and some related tutorials, can be found in the help documentation. Although the content is in English, the content is very comprehensive and illustrated. The content in the MyEclipse Learning Center is all the operation and tutorial documentation that comes with MyEclipse. 
3. Method of importing Java files 
in eclipse There are two cases for importing Java files in eclipse: 
1. If the Java files to be imported are in the WorkSpace directory of the eclipse workspace, import the Java project containing the relevant Java files into the package resource management You can: 
file (or right-click in the package explorer)—>Import—>General—>Existing project to Workspace—>Browse and select the Java project containing the relevant Java files in the WorkSpace under the selection root directory, Others do not need to be checked, just click Finish; 
2. If the Java file to be imported is in another directory, 
create a new Java project such as Hello, copy the .java file to be imported, open Hello, and then right-click under src Paste it and it will run normally; if you paste it directly under Hello, you will find that the Java file follows the JRE directory after opening, and then an error will occur when running: The editor did not find the main type. 
Finally, the imported file will work and run in the new directory (that is, under the WorkSpace), and the modification of the file in eclipse will not change the same file in other directories. 
The naming rules of java source files are as follows: if there are multiple classes in the source file, only one class can be a public class; if one class is a public class, the name of the source file must be exactly the same as the name of the class The same, the extension is .java; if the source file does not have a public class, then the source file only needs to have the same name as a class and the extension is .java.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325518875&siteId=291194637