Eclipse Notes for Summer 2016

Sesh Venugopal (with minor update by Lou Steinberg)


Using Eclipse to write Java programs



What is Eclipse?

Eclipse is an Integrated Development Environment (IDE) widely used by Java programmers. It helps with all the logistical and administrative tasks that go into developing software, therefore leaving you, the programmer, free to focus on the design of your program's logic, i.e. the data structures and algorithms. For instance, it compiles as you type--if there is a synatx error in the line you just typed, a red X shows up next to that line, along with an error message if you mouse over the X. So you can immediately fix the error before you move on. If you type an entire program and there are no red X's anywhere, your program is already compiled and you can run it.

I find that my programming time is much more effective and productive when I use Eclipse. Most of all, it is a lot more fun to program in Eclipse than to use jEdit or any other smart editor. Because Eclipse is widely used, there's a lot of third-party support in the form of "plugins". As you program more and more, you will start using these plugins for your work...

Before you install Eclipse, you want to install Java on your computer. (This also allows you to write Java programs outside of Eclipse if you choose to do so.)


Downloading and Installing Java

Install Java 1.8 on your Windows computer

Special note for Mac users


Downloading and Installing Eclipse

You can download and install Eclipse on your Windows, Mac, or Linux machine. Go to http://www.eclipse.org/downloads/ and download the Eclipse Installer for your Windows, Mac, or Linux machine, then follow the instructions to install Eclipse. (This is the latest release of Eclipse, code named Mars. Make sure you select the correct version - 32-bit or 64-bit - for your machine.)

At installation time, when asked, choose the Eclipse IDE for Java Developers package.

There are plenty of tutorials which are available from right inside the Eclipse program to help you write and run a Java program, but some of the basic tasks you will do for your programs are described in this document.

Views and Perspectives

To get started with Eclipse, a quick introduction to views and perspectives would be useful.

Workspaces

When you start Eclipse, it will ask you for a workspace, which is a folder on your machine under which all your work will be stored. You may want to create a separate workspace folder for each class. See The Eclipse Workspace section below for more details.


Writing a Java Program in Eclipse

To write a Java program in Eclipse you will need to do the following:

Point Eclipse to Java 1.8 (or 1.7/1.6/1.5, if that's what you have)

Set up a new project

Develop one or more classes in the project


The Eclipse Workspace

Every time you run Eclipse, it stores all your work in a workspace on your computer. This is a folder you can specify when you start up Eclipse. Say you specify the folder c:\eclipse\workspace as the workspace.

The projects, packages and classes you build are arranged in a hierarchy under the workspace folder. The hierarchy for the example discussed above would look like this:

                       c:\eclipse\workspace
                                |
                               Maze                             
                   _____________|____________
                   |                         |
                  src                       bin
                   |                         |
                 route                     route
                   |                         |
              Amazing.java              Amazing.class



Say you add another package called maze.util, and under it a class called BasicAlgos. Then the workspace would look like this:
 

                       c:\eclipse\workspace
                                |
                               Maze                             
                   _____________|____________
                   |                         |
                  src                       bin
          _________|_______           _______|______
          |                |          |             |
        route             maze      route          maze
          |                |          |             | 
      Amazing.java        util   Amazing.class     util
                           |                        |
                     BasicAlgos.java          BasicAlgos.class



Notice how the package name maze.util gets converted into a hierarchy of folder maze with a folder util under it: for every dot in the package name, a new level is created in the workspace folder hierarchy.


Running a Java Application

Say Amazing is an application class that has a main method.

The very first time you run an application you need to do it the long way. In the package explorer view, right click on the file that has the main method (e.g. Amazing.java), then select menu item Run As, and click on Java Application. Eclipse will open a console view (below the editor, by default) for terminal-based input and output, and run the program.

Subsequently, you can run the program by pulling down the green arrow drop down list in the task bar at the top, and selecting your application by name (e.g. Amazing)

If you are going to ask for input files in the program, these files must be placed directly below the project folder. So, for example, if the Amazing program takes input from a file called amazingin.txt, then the file is placed like this, directly under the Maxe folder:

                                c:\eclipse\workspace
                                         |
                                       Maze                             
                  _______________________|______________
                 |             |                       |
           amazingin.txt      src                     bin
                               |                       |

Setting Up Program Arguments

If you run a program that takes arguments, you need to set up the arguments for in the program properties.

Say that Amazing needs an argument which is the name of an input file from which some data will be read. (So if you were to run this program outside Eclipse, on the command line, you may type > java Amazing infile.dat)

This is how you would set up the argument in Eclipse:

IMPORTANT: If one or more of your program arguments are file names, you need to place the files directly under the project folder in Eclipse. So, if your file is stored on somewhere on your computer, you should drag and drop it into the project folder. (In the example above, you would need to drag and drop the file infile.dat into the folder Maze.)

You can now run the program using the green arrow shortcut as described in the previous section.


Zipping up a Project

If you want to zip an entire project so it can be imported in another Eclipse install in its entirety, here's what you do:


Zipping up source files

The zip file should have all the source files from all the packages in your project, placed in the respective directories.


Importing a Zipped Project Into Eclipse


Adding an External Jar File to a Project

Suppose you want to add a jar file called ext.jar to your project.

You will also see ext.jar under Referenced Libraries in your project in the Java package explorer perspective.