Introduction to Eclipse

From CompSciWiki
Revision as of 04:13, 4 April 2012 by HowardC (Talk | contribs)

Jump to: navigation, search

COMP 1010 Home > Back to Extra Labs

Introduction

In this lab, we will introduce you to using Eclipse to code your Java program in. There will be a general overview on how to install Java and Eclipse on Windows and Mac. Basic instructions on how to set up your assignments/projects in Eclipse and debugging tools that you will definitely find useful in Comp 1010 and later courses.

Installation

This installation guide will provide you with information on where to download the files and installation order. We assume that you have general knowledge on how to download/install software from the internet, so this guide will not go into step by step detail on what you need to do to install Java and Eclipse.

Installing JDK

Note: Java is already installed on Mac OS X. However, you may still want to update it to the most current version available.

For Windows user, if you currently do not have JDK ("Java SE Development Kit (JDK) 6 Update 31"), you can download from here.

Installing Eclipse

Note: Install Java first, before installing Eclipse.

  1. For Windows and Mac OS X users, Eclipse software download can be found here.
  2. Download the most current version of Eclipse Classic for your operating system.
  3. Once you have extracted the files from the download, you can find the Eclipse application within the folder. You can always come back and access the file from this location or create a short-cut and have it somewhere more accessible to you.

Running a New Program

Double-click on the Eclipse application and you will receive a Eclipse loading screen. Once Eclipse is done loading, you will be asked to select a workspace. This is where the projects (the files you will be working on) will stored. If you wish to change the location, click on Browse....

Workspace eclipse.jpg


If this is your first time opening Eclipse, you will receive a "Welcome to Eclipse" window. Click on Workbench in the top right hand corner.

On the new screen, click on File > New > Java Project in the top left hand corner. You will need get a New Project dialog box. Type in helloworld for the project name and click Finish. This is what you will now see (any new instance of Eclipse will also now go to this window instead of the "Welcome to Eclipse" window):


Projectscreen eclipse.jpg
Note: Any additional projects you create will be listed where you see the helloworld folder.


Now that you have a project folder, expand the helloworld folder and right click on src and then go New > Class.


Newsrc eclipse.jpg


In the New Java Class dialog box, enter in HelloWorld under Name and also check off public static void main(String[] args) under "Which method stubs would you like to create?", then click Finish.


Javaclass2 eclipse.jpg

Note: You will notice start your java file with a lot more if you wanted to, such as packages, modifiers, interfaces and etc.


Now add the code provided below in the main method.

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

Your window should now look like this:


Helloworld eclispe.jpg


Hint: Eclipse has a "Content Assist" feature which will display possible completions for your code, if any are available. This feature can be accessed by pressing Ctrl + Space. The feature can also appear while typing if there are any completions available for that section of code. For example:

Partialinput eclipse.jpg
Notice how in the suggestion list, you can put out after System.


To compile and run the program you can:

  1. Select Run > Run (Ctrl + F11) from the menu.
  2. Click on the Arrow eclipse.jpg button and run as java application.
  3. Right-click on HelloWorld.java and select Run As > Java Application.


Compile2 eclipse.jpg


Notice that the lower panel has now switched to Console. This is where all System input are entered in, and where System output are displayed.

Exercise