Introduction to Eclipse

From CompSciWiki
Jump to: navigation, search

COMP 1010 Home > Back to Extra Labs

Introduction

In this lab, we will introduce you to Eclipse, an integrated development environment(IDE) for writing java code. There will be a general overview on how to install Java and Eclipse on Windows and Mac. This lab contains 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 and install both the Eclipse IDE (Integrated Development Environment) and Java. 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 users, 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 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 you can create a short-cut and store it somewhere that is 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


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.

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.

Compile2 eclipse.jpg


Now that you have written your first program in Eclipse, lets take a look at a useful feature in Eclipse called "Content Assist".

Content Assist

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.

Exercise

This exercise is intended for those with basic java knowledge. The exercise will allow you to create your own java project and do some basic coding in the Eclipse environment, such as using the console for input/output and the content assist system. At the end of the exercise, you will have a general feel of how to code in Eclipse.

Instructions

  1. Create a new java project that will compare two integers (assign two variables with integers in the code) and print out the result.
  2. The two integer will be inputted by the user via the console.
  3. Compare these two integers using Math.max(a,b) and output the result to the console

Sample Solution

import java.io.*;

public class IntroToEclipse {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws NumberFormatException 
	 */
	public static void main(String[] args) throws NumberFormatException, IOException {
		// TODO Auto-generated method stub
		int a, b;
		System.out.println("Enter in a integer:");
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		a = Integer.parseInt(br.readLine());
		
		System.out.println("Enter in another integer:");
		b = Integer.parseInt(br.readLine());
		
		System.out.println("Larger number of the two: " + Math.max(a, b));
		System.out.println("***Program Completed Successfully!");
	}

}

Videos

Two videos on how to install and use the eclipse IDE.
"Installing" the IDE:
http://www.youtube.com/watch?v=CezhmO6LngY
Starting a project in Eclipse:
http://www.youtube.com/watch?v=acd3R5GQHW0

Question

What other variables other than integers can Math.max(a,b) accept? Hint: Use content assist.

double, float and long