Sunday, June 11, 2017

Using Eclipse IDE

In the last post, you learned how to create and run a basic Java application using a text editor and the command line. Although it's possible to create all Java applications with this approach, using an Integrated Development Environment (IDE) will greatly boost your productivity over using a standard text editor. IDEs have features such as automatic error checking, code formatting, syntax highlighting, integrated debugging, and auto complete. This makes writing Java programs much easier. In this post, we'll look at creating the Hello World program using an IDE.

The two main Java IDEs used in the industry are Eclipse and IntelliJ IDEA (also just called IntelliJ for short). IntelliJ has a free community edition and a paid ultimate edition, which requires a yearly fee of about $300-$500 USD. The community edition doesn't include a lot of the features in the ultimate edition, which is too expensive for most people. We are going to use Eclipse throughout this series. IntelliJ Ultimate includes a free trial version on its website if you're interested in trying it out. There are also a few other Java IDEs, such as NetBeans and BlueJ, which are less widely used.

To install Eclipse, go to the downloads page, and click on the "Download Packages" link as shown in the following screenshot:

The current version is Eclipse Neon, but new versions are released every year in June. Eclipse Oxygen is scheduled for release in late June, 2017. After going to the Download Packages page, you should see options such as these:

The two options of interest are Eclipse IDE for Java EE Developers and Eclipse IDE for Java Developers. The Java EE Developers version has everything in the Java Developers download, plus a few features for writing web applications. Either version should be sufficient for this series. To launch eclipse, simply unzip the folder that was downloaded and double-click on the eclipse program (icon with a purple circle). You may want to create a desktop shortcut for easy access in the future.

After running eclipse, you should see a prompt asking where you want to create the workspace. A workspace is a group of related projects that you are working on. For very large applications, you may need multiple workspaces, but usually one is enough. The following screenshot shows the directory for a sample workspace:

I recommend using a location such as "C:\Users\[your username]\workspace" on Windows and "/Users/[your username]/workspace" on Mac or Linux. It's a matter of preference, but what's important is that it's easy to remember where the workspace is. You'll also see a checkbox that says "Use this as the default and do not ask again". I recommend checking that so you don't see workspace prompts when launching Eclipse in the future.

After the workspace is set up, you'll be taken to the welcome page, as shown in the following screenshot:

The welcome page has several options to create a new project, update settings, etc. You can do all of this from the main page, so don't select those options. You'll also see a checkbox that says something like "Always show Welcome page at startup". I recommend unchecking that, so you don't see the startup page every time you launch Eclipse. To exit out of the welcome page, click on the "x" on the welcome tab at the top of the screen. After a moment, you should see the main workspace view, similar to the following:

Don't worry if the layout is a little different on your machine. I recommend having the Package Explorer view on the left, the Outline view on the right, and the Problems and Console view on the bottom of the screen. If you don't see those, you can select Window -> Show View -> Console/Package Explorer/Outline from the top menu, and drag that view over to the correct location on the screen. There are dozens of different views in Eclipse, and it's possible that you don't see the view you want on the list. In that case, you can select Window -> Show View -> Other, and then type the name of the view you want in the search box. In Eclipse, a view is simply a window or tab that you can use to perform various actions. Package explorer allows you to see the structure of the project, outline allows you to see the class structure, and console allows you to view the output from your program. We'll take a deeper look at views and other Eclipse options in a future post. For now, it's time to create our first program in Eclipse.

To do this, select File -> New -> Java Project from the main menu. You should see a dialog similar to the following:

Projects are used to organize different programs within your workspace. In this case, we want to create a project that we can use to learn Java basics. Under project name, type "JavaBasics". Project names should start with an uppercase letter, and must not contain spaces. You can keep the "Use Default Location" option checked, which will create the project underneath your workspace. For the JRE section, select the first radio button. You should see a version such as JavaSE-1.8 in the dropdown. Leave the Project layout and Working sets sections with the default values and click Finish. After a moment, you should see the JavaBasics project display in the Package Explorer view. Next, it is time to create our HelloWorld class.

Click on the JavaBasics project in the Package Explorer view, and select File -> New -> Class from the main menu. This will bring up the following dialog:

The source folder should be set to JavaBasics/src by default. That is the directory where the class will go under. By convention, Java source files go under the src folder. Leave the package option blank, and ignore the warning saying "The use of the default package is discouraged". We will learn about packages in a later post, so we will keep things simple and leave the package blank, even though it's not good practice. Under Name, enter "HelloWorld". This is the name of the class we are creating. Also, make sure that the public static void main (String[] args) method stub is checked and the others are not. This is important, as it allows Eclipse to automatically generate the main method, which saves us time and helps us avoid errors if we forget to add that method. Finally in the main view, we should see a tab with code similar to the following:

As we can see, the HelloWorld class and main method was automatically generated for us. How convenient! The "//TODO Auto-generated method stub" line tells us that we can put our code inside of there. The "//" character sequence is used to start a comment, which is a human-readable description that gets ignored by the Java compiler. This can be useful for other developers to understand your thought process when writing a piece of code. I recommend deleting this comment, because other developers should already know that Eclipse can automatically generate methods.

Another important feature that Eclipse provides is called autocomplete (also called intellisense). As the name suggests, Eclipse will try to give helpful guesses about what you are typing to save effort. For example, the command System.out.println("[some message]") is used in many programs to display a message on the console. Instead of having to type System.out.println each time, you can simply type sysout and hit "command + space". This will cause eclipse to automatically generate the System.Out.Println() command. Try this out by typing sysout on a blank line inside the main method, and check that Eclipse generates the full System.Out.Println() output. Very convenient! In case there are multiple options matching what you typed, Eclipse will display a dropdown with the list of possible autocomplete options. For example, try typing syso and hitting "command + space". You should see a popup similar to the following:

Simply use the up and down arrow keys to select the right option and press enter for Eclipse to do the autocomplete. In this case, we want to select the sysout option, which should be the first one in the list.

To finish our HelloWorld program, enter some System.Out.Println() statements in the main method. You should have something similar to this:

public class HelloWorld {

public static void main(String[] args) {
System.out.println("Hello World!");
System.out.println("This is my first program from Eclipse!");


To run the program, expand the JavaBasics project in the Package Explorer view, and expand the src and default package entries. Right click on the class and select Run As -> Java Application fromt the menu. If everything goes well, you should see this output in the console view:

Congratulations! You have created your first Java program from Eclipse. We've barely scratched the surface on what the Eclipse IDE can do, but we can see already see the autocomplete and code autogeneration functionality saves us time and effort. One last feature I want to point out in this post is that Eclipse can automatically detect errors and tell us what is going wrong. For example, try deleting the ";" at the end of the System.out.println("Hello World!"); line. Eclipse will underline the mistake in red squiggly line, and we can hover over that (or the red X at the beginning of the line) to see what the error is:

In this case, eclipse tells us that ";" is needed to complete block statements. You'll get better at identifying and understanding what these error messages mean as you continue to write more programs. After adding the ";" back, we can see that the error message goes away, and we're able to run our program as before. We have covered a lot of ground in this post on how to install Eclispe and run our first application. It's time to start learning about basic Java programming in the next section!


  1. Bet365 Casino Site - Live Dealer Games, Live Dealers
    We are luckyclub the largest Online Gaming supplier, Bet365 Casino offers over 20 live dealer games in a huge range of languages and 24/7 support.