Skip to content

Commit 866933d

Browse files
committed
few changes
1 parent c20eaba commit 866933d

File tree

1 file changed

+54
-24
lines changed

1 file changed

+54
-24
lines changed

app/pages/learn/01_tutorial/01_your-first-java-app/03_writing-java-applications-with-eclipse.md

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
id: first_app.eclipse
3-
title: Developing Java applications using the Eclipse IDE
3+
title: Building a Java Application in the Eclipse IDE
44
slug: learn/eclipse
55
type: tutorial
66
category: start
@@ -9,12 +9,12 @@ subheader_select: tutorials
99
main_css_id: learn
1010
toc:
1111
- Introduction and Installation {intro}
12-
- Creating Java projects {creating}
12+
- Creating a Java Project {creating}
1313
- Content Assist {content_assist}
14-
- Dealing with compilation errors and warnings {errors}
15-
- Running a program {run}
14+
- Running Your Program {run}
15+
- Dealing With Compilation Errors and Warnings {errors}
1616
- Debugging {debugging}
17-
- Generating code {generating}
17+
- Generating Code {generating}
1818
- Refactoring {refactoring}
1919
- Summary {summary}
2020
description: "Installing and getting started with the Eclipse IDE for developing Java applications"
@@ -27,26 +27,32 @@ author: ["DanielSchmid"]
2727
The Eclipse IDE (or Eclipse for short) is a commonly used application that provides tooling that helps developers write, run and debug Java code. This article describes how to get started with Eclipse for developing Java applications.
2828

2929
The easiest way to install Eclipse is to download and run the Eclipse installer from [this site](https://www.eclipse.org/downloads/packages/installer). This provides multiple options for packages to install. In most cases, `Eclipse IDE for Java Developers` is a good installation for Java development.
30+
3031
[![Eclipse Installer](/assets/images/eclipse/install.png)](/assets/images/eclipse/install.png)
3132

3233
After installing Eclipse, you can select a workspace. The workspace is the directory where most projects are located.
34+
3335
[![Workspace selection](/assets/images/eclipse/workspace_selection.png)](/assets/images/eclipse/workspace_selection.png)
3436

3537
Upon selecting a workspace, it will show a Welcome screen presenting you with mutliple options. For example, there is an option giving you an interactive tutorial showing you how to create a simple Hello-World application.
38+
3639
[![Workspace selection](/assets/images/eclipse/welcome.png)](/assets/images/eclipse/welcome.png)
3740

3841
This article will show you how to create Java projects manually so you can close the welcome-screen by clicking on the `Hide` button on the top right of the Welcome tab.
3942

4043
<a id="creating">&nbsp;</a>
41-
## Creating Java projects
44+
## Creating a Java Project
45+
46+
After installing Eclipse you should have an empty workspace. In order to create a new Java project, click on the `File` toolbar in the top left corner of the Eclipse window and select `New` > `Java Project`.
4247

43-
After installing Eclipse, you should have an empty workspace. In order to create a new Java project, click on the `File`-toolbar on the top left corner of the Eclipse window and select `New` > `Java Project`.
4448
[![File > New > Java Project](/assets/images/eclipse/file_create_project.png)](/assets/images/eclipse/file_create_project.png)
4549

46-
This will then open up a dialog that allows configuring information about the project. You will need to enter a name next to `Project name:` at the top. For example, you can choose the name `HelloWorld`. In the `Module` section at the bottom, disable the option `Create module-info.java file`. If necessary, it is possible to configure a custom Java installation in the `JRE` box.
50+
This will then open up a dialog window that allows you to configure your project. You will need to enter a name next to `Project name:` at the top. For example, you can choose the name `HelloWorld`. In the `Module` section at the bottom, disable the option `Create module-info.java file`. You can configure a custom Java installation (commonly referred to as the *JDK* or Java Development Kit) in the `JRE` box.
51+
4752
[![Java project creation dialog](/assets/images/eclipse/create_java_project.gif)](/assets/images/eclipse/create_java_project.gif)
4853

4954
This creates a Java project that is shown on the left side of the Eclipse window. When expanding this project, there should be a folder named `src`. Java classes can be created inside this directory by right-clicking on it and selecting `New` > `Class`.
55+
5056
[![New > Class](/assets/images/eclipse/create_class.png)](/assets/images/eclipse/create_class.png)
5157

5258
This opens a dialog similar to the project creation dialog. It allows specifying various options about the class you want to create. For now, you will need to enter a class name like `HelloWorld`. If you want to, you can also configure a package which can be used to group multiple classes together.
@@ -57,65 +63,82 @@ This opens a dialog similar to the project creation dialog. It allows specifying
5763

5864
Eclipse can help you write Java code by automatically completing parts of it. When pressing the key combination `Ctrl`+`Space` (or ``+`Space` on macOS or `Alt`+`/` on chinese systems) while editing Java code, Eclipse automatically suggests ways to complete the code. These suggestions can be confirmed by pressing `Enter` or double-clicking on the suggestions.
5965

60-
For example, typing `main` in a class followed by pressing `Ctrl`+`Space` suggests adding a main method.
66+
For example, typing `main` in a class followed by pressing `Ctrl`+`Space` suggests adding a main method.
67+
6168
[![Content assist suggesting a main method](/assets/images/eclipse/content_assist_main.png)](/assets/images/eclipse/content_assist_main.png)
6269

6370
Inside methods, Eclipse can suggest changing `sysout` to a `System.out.println();` statement.
71+
6472
[![Content assist suggesting a System.out statement](/assets/images/eclipse/content_assist_sysout.png)](/assets/images/eclipse/content_assist_sysout.png)
6573

6674
Furthermore, it can complete class and method names.
75+
6776
[![Content assist completing the class name String](/assets/images/eclipse/content_assist_suggest_class.png)](/assets/images/eclipse/content_assist_suggest_class.png)
77+
6878
[![Content assist completing the method String#length](/assets/images/eclipse/content_assist_suggest_method.png)](/assets/images/eclipse/content_assist_suggest_method.png)
6979

80+
81+
<a id="run">&nbsp;</a>
82+
## Running Your Program
83+
84+
In order to run a Java application, you first need to have a class with a `main` method. You can right-click the class in the package explorer or right-click in the editor where you are writing the code for the class and select `Run as` > `Java application`.
85+
86+
[![Run As > Java application in the editor](/assets/images/eclipse/run_as_editor.png)](/assets/images/eclipse/run_as_editor.png)
87+
88+
[![Run As > Java application in the editor](/assets/images/eclipse/run_as_package_explorer.png)](/assets/images/eclipse/run_as_package_explorer.png)
89+
90+
Alternatively, you can run the application using the Run [![Run button](/assets/images/eclipse/run_button.png)](/assets/images/eclipse/run_button.png) button in the toolbar. [![Run button in toolbar](/assets/images/eclipse/run_buttons_toolbar.png)](/assets/images/eclipse/run_buttons_toolbar.png)
91+
92+
When running the program, Eclipse should show the output of the program in the `Console` view.
93+
94+
[![Program with output in console](/assets/images/eclipse/console_output.png)](/assets/images/eclipse/console_output.png)
95+
7096
<a id="errors">&nbsp;</a>
71-
## Dealing with compilation errors and warnings
97+
## Dealing with Compilation Errors and Warnings
7298

7399
When Eclipse detects a compilation error, the relevant lines are underlined in red. When hovering over the line with the error or the error icon to the left of the said line, Eclipse provides information about what went wrong and also suggests how to fix the error. However, in many cases there are multiple ways to get rid of the error. You need to carefully check whether the suggestions are actually matching what you want to do. After all, IDEs cannot predict your intent.
100+
74101
[![Compilation error due to calling a non-existing method](/assets/images/eclipse/compilation_error.png)](/assets/images/eclipse/compilation_error.png)
75102

76103
Furthermore, Eclipse shows a list of errors in the `Problems` view. If this view is not displayed, it can be shown using the menu `Window` > `Show View` > `Problems`.
104+
77105
[![opening Problems view](/assets/images/eclipse/open_problems_view.png)](/assets/images/eclipse/open_problems_view.png)
106+
78107
[![Problems view showing an error](/assets/images/eclipse/problems_view.png)](/assets/images/eclipse/problems_view.png)
79108

80109
As with Errors, Eclipse can also detect code that compiles but likely contains some issues or is pointless. In this case, Eclipse will display a warning.
81-
[![Warning due to unused variable](/assets/images/eclipse/warning.png)](/assets/images/eclipse/warning.png)
82-
[![Problems view showing a warning](/assets/images/eclipse/problems_view_warning.png)](/assets/images/eclipse/problems_view_warning.png)
83110

84-
<a id="run">&nbsp;</a>
85-
## Running a program
86-
87-
In order to run a Java application, you first need to have a class with a `main` method. You can right-click the class in the package explorer or right-click in the editor where you are writing the code for the class and select `Run as` > `Java application`.
88-
[![Run As > Java application in the editor](/assets/images/eclipse/run_as_editor.png)](/assets/images/eclipse/run_as_editor.png)
89-
[![Run As > Java application in the editor](/assets/images/eclipse/run_as_package_explorer.png)](/assets/images/eclipse/run_as_package_explorer.png)
90-
91-
Alternatively, you can run the application using the Run [![Run button](/assets/images/eclipse/run_button.png)](/assets/images/eclipse/run_button.png) button in the toolbar. [![Run button in toolbar](/assets/images/eclipse/run_buttons_toolbar.png)](/assets/images/eclipse/run_buttons_toolbar.png)
111+
[![Warning due to unused variable](/assets/images/eclipse/warning.png)](/assets/images/eclipse/warning.png)
92112

93-
When running the program, Eclipse should show the output of the program in the `Console` view.
94-
[![Program with output in console](/assets/images/eclipse/console_output.png)](/assets/images/eclipse/console_output.png)
113+
[![Problems view showing a warning](/assets/images/eclipse/problems_view_warning.png)](/assets/images/eclipse/problems_view_warning.png)
95114

96115
<a id="debugging">&nbsp;</a>
97116
## Debugging
98117

99118
When a program doesn't do what you expect it to do, you might want to debug it. The process of debugging is explained in [this article](id:debugging). Eclipse provides a lot of functionality making it easy to debug Java applications.
100119

101120
In order to debug an application, you need to set a breakpoint. When the program gets to executing the line with the breakpoint, it will temporarily stop ("suspend"), allow you to inspect its current state and step through the program. To set a breakpoint, you need to double-click on the area to the left of the line you want to suspend the program at. After doing that, a blue dot should appear there.
121+
102122
[![A breakpoint next to source code](/assets/images/eclipse/breakpoint.png)](/assets/images/eclipse/breakpoint.png)
103123

104124
When running a program normally, it will ignore all breakpoints. For debugging, you need to run the program in debug mode. This can be done by clicking on the green button with the bug icon [![The debug button](/assets/images/eclipse/debug_button.png)](/assets/images/eclipse/debug_button.png) next to the run button or using `Debug As` > `Java Application`.
125+
105126
[![The debug button next to run buttons](/assets/images/eclipse/debug_button_in_toolbar.png)](/assets/images/eclipse/debug_button_in_toolbar.png)
106127

107128
When the program execution gets to a breakpoint in debug mode, Eclipse will ask you to switch to the Debug perspective. This perspective gives you more information about the program you are currently debugging so you likely want to do this and click on the `Switch` button.
129+
108130
[![Eclipse asking to switch to the Debug perspective](/assets/images/eclipse/debug_perspective_switch.png)](/assets/images/eclipse/debug_perspective_switch.png)
109131

110132
Upon opening the debug perspective, you should still see your code in the middle. However, there should be one line with a green background next to the breakpoint. This indicates the next line the program would execute. On the right side, you should see a `Variables` view containing a list of variables and their current values.
133+
111134
[![The debug perspective](/assets/images/eclipse/debug_perspective.png)](/assets/images/eclipse/debug_perspective.png)
112135

113136
While the program is suspended, you can tell it how to continue executing using buttons in the toolbar at the top.
114137
[![Buttons for controlling execution flows in the toolbar](/assets/images/eclipse/debug_toolbar_buttons.png)](/assets/images/eclipse/debug_toolbar_buttons.png)
115138
You can execute one line using `Step Over` [![Step Over button](/assets/images/eclipse/debug_step_over.png)](/assets/images/eclipse/debug_step_over.png) (`F6`), go into a method using `Step Into` [![Step Into button](/assets/images/eclipse/debug_step_into.png)](/assets/images/eclipse/debug_step_into.png) (F5) or continue executing the program until the next breakpoint with `Resume` [![Resume button](/assets/images/eclipse/debug_resume.png)](/assets/images/eclipse/debug_resume.png) (`F8`).
116139

117140
<a id="generating">&nbsp;</a>
118-
## Generating code
141+
## Generating Code
119142

120143
Sometimes you might need to write repetitive code that doesn't contain much business logic and can be generated using information from existing code. An example of this is getters/setters or `equals`/`hashCode`/`toString` methods which typically just need to access some fields. While it is often preferable to use [records](/learn/records), Eclipse allows comes with functionality to generate these pieces of repetitive code.
121144

@@ -131,9 +154,11 @@ public class Person {
131154
```
132155

133156
When right-clicking in that class, there is an option called `Source` providing various ways to generate code. Here, we can select `Generate Getters and Setters...` in order to generate accessor methods for the fields in the `Person` class.
157+
134158
[![Generate Getters and Setters](/assets/images/eclipse/context_generate_getters_setters.png)](/assets/images/eclipse/context_generate_getters_setters.png)
135159

136-
This option should open up a new window allowing us to configure which fields we want to generate accessors for. In order to create accessors for all fields, use the `Select All` button. and click `Generate` on the bottom right.
160+
This option should open up a new window allowing us to configure which fields we want to generate accessors for. In order to create accessors for all fields, use the `Select All` button. and click `Generate` on the bottom right.
161+
137162
[![Generate Getters and Setters](/assets/images/eclipse/getter_setter_modal.png)](/assets/images/eclipse/getter_setter_modal.png)
138163

139164
After doing this, the class should look as follows:
@@ -220,9 +245,11 @@ public class Person {
220245

221246
Another method that is often generated is `toString()` which returns a `String` representation of the object.
222247
To generate that method, select `Generate toString()...` in the `Source` menu.
248+
223249
[![Generate toString](/assets/images/eclipse/context_tostring.png)](/assets/images/eclipse/context_tostring.png)
224250

225251
As before, this opens a window allowing to specify options on how exactly the code should be generated.
252+
226253
[![Options for toString](/assets/images/eclipse/tostring_options.png)](/assets/images/eclipse/tostring_options.png)
227254

228255
Using the `Generate` button, Eclipse generates the `toString` method as it did with the other methods before.
@@ -281,10 +308,13 @@ public class Person {
281308
## Refactoring
282309

283310
When working on Java applications, it is often necessary to change existing code in various ways while preserving functionality. Eclipse supports developers doing that by providing various refactoring options. An example of that is renaming class, methods or fields. This can be done by clicking on a class, method or variable name, right-clicking and selecting `Refactor` > `Rename`.
311+
284312
[![Rename context menu](/assets/images/eclipse/context_rename.png)](/assets/images/eclipse/context_rename.png)
285313

286314
It is then possible to change to name to something different and confirming it using the `Enter` key. This also updates all references to the renamed element.
315+
287316
[![Renaming a class name](/assets/images/eclipse/rename_box.png)](/assets/images/eclipse/rename_box.png)
317+
288318
[![Renaming a class name](/assets/images/eclipse/rename_different_text.png)](/assets/images/eclipse/rename_different_text.png)
289319

290320

0 commit comments

Comments
 (0)