[go: up one dir, main page]

0% found this document useful (0 votes)
31 views18 pages

01 Getting Started With Eclipse (Sep 2023 Update)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views18 pages

01 Getting Started With Eclipse (Sep 2023 Update)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

CST8116 Introduction to Computer Programming

Getting Started with Eclipse: What is an IDE? Eclipse Installation and Basic Use

Last Update: February 12, 2023, D. Haley

What is an Integrated Development Environment (IDE)?


 An IDE is a program that combines several computer-programming tools into one place. You have seen use of the
Notepad text editor to enter Java code, as well as the Java Compiler (javac.exe) to compile the program, and the
Java Virtual Machine (java.exe), to run a Java program. These are all separate programming tools.
 An IDE lets you enter code, save it, compile it, and run the program, all at the click of a few buttons. In other words,
all of the tools you have been using are all in one place.
 There are other things IDEs typically provide, such as code-help, syntax-checking, code editing tools, automatic
indentation and more.
 Note that there are several IDEs for Java on the market (Visual Studio Code, NetBeans, IntelliJ and more), however
Eclipse will be used in this course. Professors may not be able to support other IDE programs if you are exploring
them, additionally your assigned homework (labs, assignments etc.) must be completed within Eclipse with the
exception of Exercise 1, which uses the basic tools.

 Before Starting installation of Eclipse, ensure you have installed and verified the configuration of Java as per the
first part of Hybrid 01
 You can find instructions on how to install Java within the Hybrid 01 materials on Brightspace.

Images used in this handout are the property of the respective owners, e.g. Eclipse.org.

Installation of Eclipse
 Visit the Eclipse.org website to download the latest version of Eclipse (https://www.eclipse.org/ )
 Tip: Use the Download button in the upper right corner, then use the “Download Packages” link to see more
download options (https://www.eclipse.org/downloads/ )

 While Eclipse has an optional installer available we will not use it, instead we will select a specific version of Eclipse
and download a Zip file instead. Use the download-packages link.

Page 1 of 18
 On the download packages page (https://www.eclipse.org/downloads/packages/ ) get this download:
 “Eclipse IDE for Java Developers”

 At the time this handout was created the downloaded file is named: eclipse-java-2021-03-R-win32-x86_64.zip

1) Unzip the zip file you downloaded (Graphics Icon may differ)

2) There should be a folder extracted named “eclipse”, it is a large file it may take a few moments to extract.

3) Cut and Paste, or Move the eclipse folder to C drive.


4) Inside the eclipse folder, right-click on eclipse.exe and select one or all of:
a. Pin to Start
b. Send to: Desktop (Create Shortcut)
c. Pin to taskbar

Page 2 of 18
 At this point Installation is complete.

 We did not need to use an installation program to install Eclipse. This also means we do not need to uninstall Eclipse
either when we are done with it, and there are no hidden changes to the operating system configuration (typically
done by an installer program).

 In future to update eclipse:


o Save all work and close down Eclipse.
o Backup the workspace folder with your projects within it.
o Delete the eclipse folder from C drive.
o Download, Unzip, copy the new eclipse folder to C drive.
o Remap your Start Menu, re-create the desktop shortcut, remap pin to taskbar if needed.
o Done.

Create a Workspace Folder Before Starting Eclipse:


 Eclipse creates project folders for each programming project you work on, even a program as simple as
HelloWorld.java must be within a Project folder for Eclipse to properly edit, compile, and run it.
 All project folders are created inside of a folder on the hard drive called a workspace folder.
 Before starting Eclipse for the first time, create a folder within your C:\CST8116 Homework folder named:
o CST8116 Eclipse Workspace
 Full Path is: C:\CST8116 Homework\CST8116 Eclipse Workspace
 Back this folder up, i.e. copy it, to an external USB memory stick each week and store it away from your computer
and / or computer bag (backpack, briefcase etc.) i.e. do not carry the memory stick around with your computer. If
your laptop fails, or gets lost or stolen, your homework will be safe.

Basic Use of Eclipse

Starting Eclipse
 Start Eclipse using the shortcut, pinned link, or double-click on the eclipse.exe directly. The splash screen is the first
thing you will see. (Splash screen appearance may vary depending on version of Eclipse installed).

Page 3 of 18
Selecting a Workspace
 Eclipse will ask you for the workspace directory, browse to the directory you created in your homework folder and
Eclipse will configure it as a workspace by adding the needed files. Click Launch.
(I do not recommend using the “Use this as the default and do not ask again” as I like the option to choose which
workspace I will use (I have more than one), on program start-up and clicking Launch is not time consuming.)
Note that in the image below, the default workspace location was changed to
“C:\CST8116 Homework\CST8116 Eclipse Workspace” if this location does not yet exist, Eclipse will create it.

 Use the Launch button to start Eclipse

There will be a loading bar / splash screen

Page 4 of 18
Welcome Page
 The Welcome Page is displayed by default.

 A built-in reference is the help system. Use the Help Menu and select the Help Contents sub-menu to open the help
page. In this screen shot the relevant help sections top categories were expanded, these are the Workbench User
Guide, and the Java development user guide.

Page 5 of 18
 There is too much to learn in one sitting by reading all of the help system, use it as a reference to learn more as
needed about Eclipse.

The Tips and Tricks can tell you how to use a dark theme for Eclipse. Your professor may use the default color theme
with the font size adjusted for lectures.

The Getting Started, Concepts, Tasks sections contain useful information.

Accessibility Features in Eclipse


 More importantly, to get information about Accessibility features in Eclipse:
 Use Help Menu, Use Help Contents sub-menu, expand the Workbench User Guide menu item, expand the Concepts
menu item, expand the Accessibility features in Eclipse menu item then review content under these menu items:
o Navigating the user interface using the keyboard.
o Accessibility features in textual editors.
o Fonts and colors in Eclipse.
Page 6 of 18
Closing and Opening the Welcome Page
 If you close the welcome page and would like to open it again, use the Help Menu, and select the Welcome sub-
menu.

Opening the Workbench


 There is an icon on the Title Bar of the Welcome page, at the furthest right side that looks like an orange box with a
small orange arrow in it with the word “hide” beneath. Click this to open the workbench.

Page 7 of 18
 Alternatively, you can close the Welcome page using the X icon within the welcome page’s tab.

The Java Perspective


 By default, the Java Perspective will open.
 A Perspective simply changes what views are shown on screen, what tools are available, how the views are
organized (etc.) according to pre-set settings.
 Your Eclipse environment should have a Package Explorer view on the Left side, a large blank grey area in the
middle, Task List, and Outline views on the right hand side and tabs at the bottom of the screen titled from left to
right Problems, Javadoc, Declaration. (Image on next page)

Eclipse Console View


 Using the Window menu, Show View sub menu, select the Console View. It should be added to the tabbed views at
the bottom of the screen.
 Note that if you close any view by accident, you can use the same set of menus as per opening a console view to re-
open a lost view.

Page 8 of 18
Specifying Amazon Corretto JDK 17 as the default for the workspace
 Use the Window Menu, Preferences sub-menu
 A dialog window will open, select Java and expand it using the little arrow to the left of “Java”
 Select the “Installed JREs”

 Here, there is no entry in the list for C:\AmazonCorretto\


 Use the Search… button

 Navigate to C:\AmazonCorretto\ and use the select folder button:

 Remove the check mark from the eclipse plugins jre, and select instead AmazonCorretto\jdk17.0.3_6
(If you are using a newer version of Amazon Corretto 17 than 17.0.3_6 use that.)
 The screen shot below shows this change:

 Click the Apply and Close Button

Page 9 of 18
Setting the Compiler Compliance Level for Java 17
 To ensure that Eclipse gives us syntax error messages appropriate for Java 17 we need to set the compiler
compliance level. Versions of Java before 9 are listed with their version number, e.g. Java 8 is listed as 1.8, while Java
9 onwards is listed as we would expect (Java 9, 10 and so on).
 Use the Window > Preferences menu to re-open the preferences dialog from the previous step, click on the
Compiler option under Java, then use the Drop-down box to verify Java 17, or to select Java 17.

 Click the Apply and Close Button

Page 10 of 18
Creating Your First Java Project
Using the File Menu, use the New sub-menu and select Java Project.

 I used a project name of “Week 1 Hybrid Demo Hello World”.


o Project names can contain spaces, should be brief yet descriptive.
 Then I selected “Use default JRE ‘jdk17.03_6’ and workspace compiler preferences’
 Leaving everything else unchanged I used the Finish button.
 Uncheck the option to create a module-info.java file.
o Modules are an advanced Java feature added with Java 9; we have no use for this feature in an Introductory
Programming Course so will not use or address it here (in this course).

Page 11 of 18
 Eclipse will return to the Java Perspective, I used the little > character next to the project name (Week 1 Hybrid
Demo Hello World) to expand the folder list.
o This is not the same as the physical files on the hard drive; Eclipse is showing you what you need to see
within the Package Explorer.
 There is a folder named src, short for “Source” which is really a short way of saying for “Source Code”
o Java source code files will be placed into this folder.

 Select the src folder within your project, in the package explorer, then
 Using the File menu, select New sub-menu, then select the Class sub-menu.

 I entered the class name “HelloWorld” (do not use spaces in a class name) within the Name text box, placed a
check-mark next to “public static void main(String[] args)” and clicked the Finish Button. Everything else was left with
defaults.
o On an aside, using the comments templates may be a neat way to place your required comment block at the
top of each class-source-code file you create, but this is something you will need to explore later.
o * There are a number of options on this window, and much you will not understand. For now just know that
as you learn more Java including in future courses more of the options you are seeing will make sense.
(Week 1 Lecture Notes explain what method main is used for).

Page 12 of 18
 Note the warning about using the default package is discouraged. For now, ignore this, we will learn more about
packages later.
 A package is a way to organize classes, similar to how you organize files within folders on a hard drive.
o Because each of our programs in Introduction to Computer Programming will not have many source code
files we can use the default package, in other words organizing a few code files in a folder is manageable.
o Larger projects consisting of 100’s of source code files need a package structure to organize the files.

Page 13 of 18
 Finally, we have our first Java program opened within the Eclipse editor.
 The package explorer shows the file HelloWorld.java on the left. (1)
 The editor view shows us the contents of the file, and uses the file name as the tab name (2)
 The outline view shows class HelloWorld, with method main. In larger programs, the outline view helps you to
navigate code files, as well as sections within code files. (3)

 Edit the source code by removing the programmer comment and adding System.out.println("Hello World"); within
method main as illustrated below.
 A programmer comment starts with two forward-slash characters, and by default has green text in Eclipse.
e.g. // TODO Auto-generated method stub

 Here is the code before; I removed the default coloring here to make it easier to read.
public class HelloWorld {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}

 Change it to this (shown below), including the comment block at the top of the file, tip once you start typing /*
eclipse will align and add * characters as needed for you.
 Click on the space beside “1” above public class HelloWorld and add a “Header comment” as well as a comment
above the method main header public static void main(String[] args) (as illustrated below)
/*
* Student Name: Stanley Pieda
* Lab Professor: Professor Stanley Pieda
* Due Date: no due date
* Modified: June 23, 2022.
* Description: Hybrid 01 Demo file
*/

Page 14 of 18
/* Description: this class outputs a greeting from within method main. */
public class HelloWorld {

/* Entry point for the application. */


public static void main(String[] args) {

System.out.println("Hello World");

}
}
 As you type in System.out.println("Hello World"); type slowly and pause after entering the first dot character, Eclipse
will offer code-help to assist. (Complete the line by typing System.out.println("Hello World");

Cautionary Note:
 Eclipse’s productivity enhancement features, code helpers, a check box to create method main and more help you
write code, but also interfere with your learning. Without looking at anything, can you write the main method
correctly in the space below and get it perfectly correct including capitalization?

Challenge: Write method main here from memory:

 You may want to practice coding by hand, as well as using Eclipse. The theory tests in the course may require you
to write code by hand and remember syntax.

 In addition, sometimes Eclipse does not really understand what you are trying to do, and the automated features
can make things worse.

Compiling a Java Program in Eclipse


 Eclipse is periodically running a compiler on your code in the background to detect mistakes, try adding a space
within the HelloWorld class name; the red underline shows the presence of a syntax error, floating the mouse over
the red line will provide a tool-tip under the mouse with the error message. In addition, apparently a different
message for each word.
o Hello is reported as “The public type Hello must be defined in its own file”. i.e. public class names must be
the same as the file name.

Page 15 of 18
o World is reported as “Syntax error on token “World”, { expected” i.e. the class name must be one word
followed by only an opening brace{

 There are suggested fixes like renaming the code file, or rename the type to HelloWorld. Often none of the
suggested fixes is correct. (See below, I experimented).
 The secret here is that the problem the complier reports could be treated as “Hello, message from the compiler,
there was something I didn’t understand at this point in the source code please fix” it then suggests a possible
problem but you need to look at your code and do some thinking to fix the mistake.
 Make lots of mistakes as you work, then fix them, you will make fewer mistakes, and get faster at fixing them.
This comes with practice. It is better to learn how to fix mistakes in programming, than attempt to write perfect
code the first time especially as a learner. (Can you learn to ride a bicycle from a book, without practicing on a
bicycle at all, and expect to ride perfectly your first time?)

 Out of curiosity, I experimented with the quick-fix of “Rename type to “HelloWorld” and sadly, Eclipse only changed
“Hello” to “HelloWorld” leaving the original “Word” in place, so it didn’t fix things it made them a little worse:

public class HelloWorld World {

 Correct the syntax mistake by removing the space, and ensuring that the only class name after the word class is
“HelloWorld”.

 On an aside, using the Project menu, you can see that Build Automatically is selected. If you deselect this check box,
you would then be required to run the compiler using the Project menu manually. (Not recommended).
(Build in this context for us in an introduction course means to compile, but Eclipse is doing a few more things
behind the scenes)

Page 16 of 18
Running a Java Program in Eclipse
 Use the Run menu’s Run sub-menu to attempt to run the program

 Or use the run icon in the toolbar (float the mouse over buttons to see tool tips)

 If there are no syntax mistakes your program should run, and you will see the output in the console view (a tab at
the bottom of the screen)
o Eclipse may ask you if you want to save sources (source code files) before you run them, I usually click OK to
save, or alternatively you may want to set the check box “Always save resources before launching” to on.

 Because this program has no user input, or much processing, it shuts down after outputting Hello World.
 There is a small grey box-icon in the Console tool-bar that lights up Red when a program is running; this button shuts
down a buggy program that stops responding. If you float the mouse over it the tool-tip is “Terminate”.
 The double XX button removes the terminated program launches from the console to remove clutter. And the tool-
tip is “Remove All Terminated Launches”
 Tip: Use the Terminate and Remove All Terminated Launches buttons. If you have Java programs that bug out and
stop responding they still use CPU and RAM, so repeatedly running the same buggy program again and again is
similar to running MS Word or your web browser multiple times and leaving them open. Theoretically, too many
running programs can use up your CPU time, your RAM, and slow down your computer. So Terminate and Remove
Terminated Launches as needed.

Caution: Save your work often.


 The author of this document lost about 8 hours of programming homework as a student, because he skipped this
step: Save your work often, he was pulling a ‘start at the last minute’ on a homework assignment, and consequently
was up all night. Then the IDE crashed his (my) computer at 5:00 am and I discovered nothing was saved….
Apparently, in 8 hours I had not saved my work once. Therefore, I did not do well on that homework assignment,
which was due at 8:00 am that same morning.
 Eclipse has a hint that a file is not saved, an asterisk * is placed at the start of the tab name of unsaved files. So, use
the File menu and Save-All sub-menu frequently while working, or the Save-All button in the toolbar.

Page 17 of 18
Summary: What is an IDE?
 We have installed and used fundamental features of the IDE Eclipse.
 We have used the Editor in Eclipse, instead of windows Notepad.
 We have automatically used the compiler, instead of javac on a command prompt window.
 We have viewed output in a console view inside Eclipse, instead of using java in a command prompt window.
 In other words, our basic development tools of Editor, Compiler, and Running a program are all within Eclipse.

Page 18 of 18

You might also like