“How to” manual for developing programs in Eclipse
Objectives
After completing this manual, you should understand and gain experience with the following:
•Become familiar with Eclipse’s user interface.
•Be able to create projects, edit, compile, and run programs in Eclipse.
•Understand the file structure used by Eclipse.
Introduction
Eclipse is an Integrated Development Environment (IDE) for developing Java programs. This means all the tools you need to create a java program (such as the editor, project debugger, and helper) are integrated into a single graphical user interface instead of operating as separate programs (such as with Sun’s JDK commands). This manual introduces how to create a new project, edit, compile, and run Java programs on Eclipse.
Procedure
Launch Eclipse
Locate and double click the Eclipse icon on the desktop. If there is no desktop icon go to the Start Menu, click “All Programs”, then click “Eclipse.” The first time you run the program it will ask you to select a workspace. If you are not asked to select a workspace skip this section we will correct it later.
The workspace is the location on the drive where Eclipse will store all project folders and files. For CS 160/161 we ask that you set the workspace to either the floppy drive or a flash drive in a folder called workspace. If you do not have a workspace folder on your disk Eclipse will create one for you. Just fill out the box like the example above replacing drive “E” with the drive letter you wish to use. The next screen you will see the first time the program is launched, is the welcome screen.
Click the “x” on the tab or click on the workbench icon to go to the main program screen.
NOTE
If you did not start with the workspace launcher, follow the next steps to make Eclipse prompt for a workspace. Using the default workspace will leave your project open to anyone that accesses the computers in the labs, and it is your responsibility to make sure your labs and projects are not changed by someone other than yourself.
Starting from the main screen, choose Window -> Preferences
On the Preferences popup window, expand the “General” section by clicking the arrow sign next to it, expand the “Startup and Shutdown” section, click on “Workspaces” and check the box next to “Prompt for workspace at startup”. Click “OK”.
Close the Eclipse application and launch it again. It will prompt you the workspace location.
Eclipse IDE
The main user interface consists of a few key parts. Along the top of the screen is the tool bar which contains buttons to create, save, and run a project. The left side of the screen has the package explorer. All the projects in the workspace can be seen here and you can also view any classes contained in the project. In the middle is the editor, which is where you will write and edit your java source code. The right side of the screen has an outline which shows you an outline of the currently active project and along the bottom is the console which will display the output of the program.
Creating a new project
A project is a container that ties all the files together. The information about each Eclipse project is stored in a project file with the .project extension. This project file contains a list of all the files and project settings and properties. This information allows projects to be imported into other workspaces.
1. In the file menu choose “New” -> “Project…”
2. In the New Project wizard, select “Java Project”, and click “Next.”
3. Creating a project
4. On the “New Java Project” window, enter a project name and click “Finish”. The directory where the project is stored is /workspace/project_name by default. This folder is what you will turn in for projects and labs.
If presented the prompt below, check “Remember my decision” and click “Yes”.
Creating a new Java class
1. To create a class in your project, click “File” -> “New” -> “Class” or right click on the name of your project in the package explorer choose “New” -> “Class”.
2. On the “New Java Class” popup window, enter the name of the class. The class name should give an indication of what code is contained in the class. To make a driver class, you should also place a check mark next to “public static void main(String[] args)” This creates the main method for you in the class. Click “Finish”.
3. Eclipse will add your new Java class as a tab in the main window.
Compiling the Source Code
Compiling the source code turns the code that we can read and understand into bytecode. In the “Project” menu, if you see a checkmark next to “Build Automatically,” then Eclipse will automatically compile your code for you when you save.
Correcting the Compilation Errors
If there are errors in your source code such as typos or other mistakes, Eclipse will mark the line with an error marker (red colored underline). If you save a class that contains errors then Eclipse will place a red X next to the class name. A list of errors will also be displayed in the bottom pane in the problems tab
If you double click on the error it will take you to the statement that is causing the error. You can also click the error icon on the line and it will give you options for fixing the error. NOTE: The solutions given by Eclipse may not be the solution you need to fix your problem.
Run Application
All we need to do to run the application is to click the “Run” button on the tool bar.
The bottom of the screen shows the results of running the program in the console tab. If you have problems running the program right click on the class name in the “Package Explorer” and point to “Run As” -> “Java Application”.
Saving your work
Saving your work is very important. Save often. You can save your work by click the “Save” icon on the tool bar (the one that looks like a disk), or you can press “Ctrl + s” on your keyboard to save the active class. To save all your classes at once press “Ctrl + Shift + s” on your keyboard or click “File” -> “Save All”. If you try to run a program that has not been saved, Eclipse will ask you if you want to save it first. You can choose then to always have it save your files before it runs them.
Project Management
1.When you create projects in Eclipse, it remembers them, so each time you start Eclipse, all projects you have created under your workspace will be shown in the “Package Explorer” pane. There is no need to close or re-open projects because of this. Also, Eclipse remembers which tabs were open from your previous sessions.
2.If you no longer wish to see a project in your “Package Explorer” pane, you can right click on the project and choose “Delete”. You will see a popup similar to the image below, leaving the box unchecked simply unlinks the project from Eclipse without deleting your program. This means that the project can be imported back into Eclipse at a later point. If you check the box, the files for that project will be deleted as well, so it is best to leave it unchecked, unless you are absolutely sure you no longer need the project. Importing Projects
If you wish to work on a project from multiple locations (different PCs), you will need to import the project into Eclipse each time you switch machines. To import a project, copy the project folder (workspace/project_name/) to the workspace directory of the new computer you wish to access your project on. Start Eclipse on that machine and choose “File” -> “Import”, from the toolbar.
On the “Import” popup, under General, choose “Existing Projects into Workspace” and click “Next”. On the next window click “Browse”, for the root directory location. Find the location you copied the project to, select it, and hit “OK” then “Finish”.
Traveling with Eclipse
Eclipse can be installed on a flash drive that has at least 256mb. This allows you to take a full version of the application and all your project files with you on your flash drive.
1.Open a web browser to the following URL, http://www.eclipse.org/downloads/
Next, find the section “Eclipse IDE for Java Developers” and download the version of Eclipse for the OS you intend to use.
2.The downloaded file is a compressed archive of the Eclipse installation; extract this to the root directory of your flash drive. If you extracted the archive to your flash drive, you will have a new folder, FlashDrive:/eclipse. Inside the eclipse folder is eclipse.exe, which will start Eclipse.
3.The first time the program runs, it will prompt you to select a workspace. If you create your workspace on the same flash drive you extracted the program files to, you can take that flash drive to any computer and run Eclipse with your workspace.