Welcome!

Eclipse Authors: Jayaram Krishnaswamy, Yeshim Deniz, Liz McMillan, Shelly Palmer, Si Chen

Related Topics: Eclipse

Eclipse: Article

SYS-CON Webcast: Eclipse IDE for Students, Useful Eclipse Tips & Tricks

Lesson 10 In The Hugely Popular "Java Basics" SYS-CON.TV Education Series by Yakov Fain

In Lesson 10 of the Java Basics series Yakov Fain shows you how to start working with Eclipse IDE, which is a tool of choice for millions of professional Java programmers. After reading this article you may want to look at another of Yakov's articles for youngsters "Teaching Kids Programming: Even Younger Kids can Learn Java".

Moving to Eclipse

Programmers usually work in a so-called Integrated Development Environment (IDE). You can write, compile and run programs there. An IDE also has a Help thingy that describes all elements of the language, and makes it easier to find and fix errors in your programs. While some IDE programs are expensive, there is an excellent free IDE called Eclipse. You can download it from the Web site www.eclipse.org. In this chapter I'll help you to download and install Eclipse IDE on your computer, create a project there called Hello World, and after this we'll be creating all our programs there.

Make yourself comfortable in Eclipse - it's an excellent tool that many professional Java programmers use.

Installing Eclipse

Open the Web page www.eclipse.org and click on the Download menu on the left (http). Click on the link Main Eclipse Download Site and select the version of Eclipse you want to download. They usually have one latest release and several stable builds. The latest release is an officially released product. Even though stable builds may have more features, they still may have some minor problems. At the time of this writing the latest stable build is 3.0M8.

Select this build and you'll see the following window:

 

Click on the link (http) next to the word Windows, Mac, or Linux depending on your computer, and download the file with this long name that ends with .zip to any folder on your disk.

 

 

Installation of Eclipse is complete! For your convenience, create the shortcut for Eclipse on your desktop. Right-click on the Windows desktop, then press New, Shortcut, Browse, and select the file eclipse.exe in the folder c:\eclipse. To start the program, double-click on the blue icon Eclipse, and you'll see the first Welcome screen (this screen is changing slightly with each Eclipse build):

 

If your screen looks different, proceed to so-called Workbench, which is the working area for your Java projects.

Getting Started with Eclipse

In this section I'll show you how you can quickly create and run Java programs in Eclipse. You can also find a nice tutorial under the menus Help, Help Contents, and Java Development User Guide.

To start working on a program you'll need to create a new project. A simple project like our HelloWorld will have just one file - HelloWorld.java. Pretty soon we'll create more advanced projects that will consist of several files.

To create a brand new project in Eclipse just click on the menus File, New, Project, and then press the button Next on the New Project window. Now you'll need to enter the name of your new project, for example My First Project:

 

Look at the grayed out box Directory. It tells you where the files of this project will be located on the disk. Eclipse has a special folder workspace, where it keeps all files for your projects. Later on, you can create separate projects for a calculator program, a Tic-Tac-Toe game, and other programs.

Eclipse workbench has several smaller areas called perspectives which are different views of your projects.

 

If you click on the little plus sign by My First Project, it'll expand showing you an item Java Run-time Environment (JRE) System Library which is a part of the project. If for any reason you do not see JRE there, click on the menus Windows, Preferences, Java, Editor, Installed JREs, Add, and, using the button Browse find the folder where you have installed Java, for example c:\j2sdk1.5.0.

Creating Programs in Eclipse

Let's recreate the HelloWorld program from Chapter 1 of my e-book Java Programming for Kids, Parents and Grandparents in Eclipse. Java programs are classes that represent objects from real life. You'll learn more about classes in the next chapter.

To create a class in Eclipse select the menus File, New, Class and enter HelloWorld in the field Name. Also, in the section Which methods stubs you would like to create, check off the box

public static void main(String[] args)

 

Press the button Finish, and you'll see that Eclipse created for you the class HelloWorld. It placed program comments (the text between /* and */) on top - you should change them to describe your class. After the comments you'll find the code of the class HelloWorld with an empty method main(). The word method means action. To run a Java class as a program, this class must have a method called main().

 

To complete our program, place the cursor after the curly brace in the line with main, push the button Enter and type the following on the new line:

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

To save the program on disk and compile it, just press at the same time two buttons on your keyboard: Ctrl-S. If you did not make any syntax errors, you won't see any messages - the program is compiled. But let's make an error on purpose to see what's going to happen. Erase the last curly brace and hit Ctrl-S again. Eclipse will display the Unmatched Brace error in the tasks perspective, and also it will place a red mark by the line that has a problem.

As your projects become larger, they'll have several files and compiler may generate more than one error. You can quickly find (not fix though) the problematic lines by double-clicking on the error message in the tasks perspective. Let's put the curly brace back and hit Ctrl-S again - voila, the error message is gone!

Running HelloWorld in Eclipse

Our simple program is a one-class project. But pretty soon your projects will have several Java classes. That's why before running the project for the first time, you need to tell Eclipse which class in this project is the main one.

Select the menu Run, then Run...(make sure that Java Application is selected in the top left corner), and enter the names of the project and the main class:

 

Now press the button Run, to start the program. It will print the words Hello World in the console view the same way as it did in Chapter 1.

You can run this project by selecting the menus Run, Run Last Launched or by pressing the buttons Ctrl-F11 on the keyboard.

How HelloWorld Works?

Let's start learning what's actually happening in the program HelloWorld.

The class HelloWorld has only one method main(), which is an entry point of a Java application (program). You can tell that main is a method, because it has parentheses after the word main. Methods can call (use) other methods, for example our method main() calls the method println() to display the text Hello World on the screen.

Each method starts with a declaration line called a method signature:

public static void main(String[] args)

This method signature tells us the following:

  • Who can access the method - public. The keyword public means that the method main() could be accessed by any other Java class or JVM itself.
  • Instructions on how to use it - static. The keyword static means that you don't have to create an instance (a copy ) of HelloWorld object in memory to use this method. We'll talk about instances more in the next chapter.
  • Does the method return any data? The keyword void means that the method main() doesn't return any data to the calling program, which is Eclipse in this case. But if for example, a method had to perform some calculations, it could have returned a resulting number to its caller.
  • The name of the method is main.
  • The list of arguments - some data that could be given to the method - String[] args. In the method main() the String[] args means that this method can receive an array of Strings that represent text data. The values that are being passed to a method are called arguments.

As I said before, you can have a program that consists of several classes, but one of them has the method main(). Java class usually have several methods. For example, a class Game can have the methods startGame(), stopGame(), readScore(), and so on.

The body of our method main()has only one line :

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

Every command or a method call must end with a semicolon ;. The method println()knows how to print data on the system console (command window). Method names in Java are always followed by parentheses. If you see a method with empty parentheses, this means that this method does not have any arguments.

The System.out means that the variable out is defined inside the class System that comes with Java. How are you supposed to know that there's something called out in the class System? Eclipse will help you with this. After you type the word System and a dot, Eclipse will show you everything that is available in this class. At any time you can also put a cursor after the dot and press Ctrl-Space to bring up a help box similar to this one:

 

The out.println() tells us that there is an object represented by a variable out and this "something called out" has a method called println(). The dot between a class and a method name means that this method exists inside this class. Say you have a class PingPongGame that has a method saveScore(). This is how you can call this method for Dave who won three games:

PingPongGame.saveScore("Dave", 3);

Again, the data between parentheses are called arguments or parameters. These parameters are given to a method for some kind of processing, for example saving data on the disk. The method saveScore() has two arguments -a text string "Dave", and the number 3.

Eclipse will add fun to writing Java programs. The Appendix below has some useful tips and tricks that will speed up your Java programming in this excellent IDE.

Appendix: Eclipse Tips

Eclipse has many little convenient commands that make Java programming a little easier. I've included some useful Eclipse tips here, but I'm sure you'll find more when you start using this tool.

  • If you see a little asterisk in the tab with the class, this means that the class has unsaved code changes.
  • Highlight the name of the class or a method that is used in your code and press the button F3 on your keyboard. This will take you to the line where this class or method was declared.
  • If some of the lines are marked with red error circles, move the mouse over the circle to see the error text.
  • Press Ctrl-F11 to run the last-launched program again.
  • Place the cursor after a curly brace and Eclipse will mark the matching brace.
  • To change the superclass when creating a new class, click on the button Browse, delete the class java.lang.Object and enter the first letter of the class you'd like to use. You'll see a list of available classes to choose from.
  • To copy a class from one package to another, select the class and press Ctrl-C. Select the destination package and press Ctrl-V.
  • To rename a class, a variable or a method, right-click on it and select Refactor and Rename from the popup menu. This will rename every occurrence of this name.
  • If your project needs some external jars, right-click on the project name, select Properties, Java Build Path and press the button Add External Jars.

Eclipse Debugger

Rumor has it that about 40 years ago, when computers were large and would not even fit in your room, all of a sudden one of the programs started giving wrong results. All these troubles were caused by a small bug that was sitting inside the computer somewhere in the wires. When people removed the bug, the program started working properly again. Since then, to debug a program means to find out why it does not give the expected results.

Do not confuse bugs with the compilation errors. Say for example, instead of multiplying the variable by 2, you'll multiply it by 22. This typo will not generate any compilation errors, but the result will be incorrect. Debuggers allow you to step through a running program one line at a time, and you can see and change values of all variables at each point of the program execution.

 

I'll show you how to use Eclipse debugger using the FishMaster program from Chapter 4 of my e-book  Java Programming for Kids, Parents and Grandparents.

A breakpoint is a line in the code where you'd like program to pause so you can see/change current values of the variables, and some other run-time information. To set a breakpoint just double click on the gray area to the left of the line where you want a program to stop. Let's do it in the FishMaster class on the line myFish.dive(2). You'll see a round bullet on this line which is a breakpoint. Now, select the menus Run, Debug.... Select the application FishMaster and press the button Debug.

FishMaster will start running in the debug mode, and as soon as the program reaches the line myFish.dive(2), it will stop and will wait for your further instructions.

You will see a window similar to the next one.

 

In the left bottom part of the debug perspective, you see that the line with the breakpoint is highlighted. The blue arrow points at the line that is about to be executed. On the right side (in the Variables view) click on the little plus sign by the variable myFish. Since this variable points at the object Fish, you will see all member variables of this class and their current values, for example currentDepth=20.

The arrows in the top left area allow you to continue execution of the program in different modes. The first bended yellow arrow means step into the method. If you press this arrow (or F5), you'll find yourself inside the method dive(). The window changes and you see the values of the argument howDeep=2 as in the next screenshot. Click on the little plus by the word this to see what are the current values of member variables of this object.

To change the value of the variable, right-click on the variable and enter the new value. This can help when you are not sure why the program does not work correctly and would like to play what if game.

 

To continue execution one line at a time, keep pressing the next arrow step over (or the button F6).

If you want to continue program in the fast mode, press a small green triangle or the button F8.

To remove the breakpoint just double-click on the little round bullet and it'll disappear. I like using debugger even if my program does not have a bug - it helps me better understand what exactly happens inside the running program.

Where to put a breakpoint? If you have an idea which method gives you problems, put it right before the suspicious line of code. If you are not sure, just put it in the first line of the method main() and slowly walk through the program.

 

More Stories By Yakov Fain

Yakov Fain is a co-founder of two software companies: Farata Systems and SuranceBay. He authored several technical books and lots of articles on software development. Yakov is Java Champion (https://java-champions.java.net). He leads leads Princeton Java Users Group. Two of Yakov's books will go in print this year: "Enterprise Web Development" (O'Reilly) and "Java For Kids" (No Starch Press).

Comments (21) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
JDJ Reader against plagiarism. 02/17/06 09:47:39 AM EST

It appears that your original articles are being plagiarized (republished without attribution to you, and probably without your permission) at http://www.javaprogrammingworld.com/

For example, Lesson 1 at http://www.javaprogrammingworld.com/chap1.htm seems to be exactly your words, only the graphics are omitted.

I stand to be corrected, but you (as author) and SYS-CON (as publisher) might want to pursue this with the owner of the site www.javaprogrammingworld.com

JDJ News Desk 11/13/05 09:40:25 PM EST

SYS-CON.TV Webcast: Eclipse IDE For Students, Useful Eclipse Tips and Tricks. Programmers usually work in a so-called Integrated Development Environment (IDE). You can write, compile and run programs there. An IDE also has a Help thingy that describes all elements of the language, and makes it easier to find and fix errors in your programs. While some IDE programs are expensive, there is an excellent free IDE called Eclipse.

SYS-CON UK News Desk 11/13/05 09:14:16 PM EST

SYS-CON.TV Webcast: Eclipse IDE For Students, Useful Eclipse Tips and Tricks. Programmers usually work in a so-called Integrated Development Environment (IDE). You can write, compile and run programs there. An IDE also has a Help thingy that describes all elements of the language, and makes it easier to find and fix errors in your programs. While some IDE programs are expensive, there is an excellent free IDE called Eclipse.

SYS-CON UK News Desk 11/13/05 09:14:16 PM EST

SYS-CON.TV Webcast: Eclipse IDE For Students, Useful Eclipse Tips and Tricks. Programmers usually work in a so-called Integrated Development Environment (IDE). You can write, compile and run programs there. An IDE also has a Help thingy that describes all elements of the language, and makes it easier to find and fix errors in your programs. While some IDE programs are expensive, there is an excellent free IDE called Eclipse.

JDJ News Desk 11/13/05 08:41:46 PM EST

SYS-CON.TV Webcast: Eclipse IDE For Students, Useful Eclipse Tips and Tricks. Programmers usually work in a so-called Integrated Development Environment (IDE). You can write, compile and run programs there. An IDE also has a Help thingy that describes all elements of the language, and makes it easier to find and fix errors in your programs. While some IDE programs are expensive, there is an excellent free IDE called Eclipse.

ITSG News Desk 11/13/05 08:17:06 PM EST

SYS-CON.TV Webcast: Eclipse IDE For Students, Useful Eclipse Tips and Tricks. Programmers usually work in a so-called Integrated Development Environment (IDE). You can write, compile and run programs there. An IDE also has a Help thingy that describes all elements of the language, and makes it easier to find and fix errors in your programs. While some IDE programs are expensive, there is an excellent free IDE called Eclipse.

ISSJ News Desk 11/13/05 08:13:35 PM EST

SYS-CON.TV Webcast: Eclipse IDE For Students, Useful Eclipse Tips and Tricks. Programmers usually work in a so-called Integrated Development Environment (IDE). You can write, compile and run programs there. An IDE also has a Help thingy that describes all elements of the language, and makes it easier to find and fix errors in your programs. While some IDE programs are expensive, there is an excellent free IDE called Eclipse.

Eclipse Developer's Journal News Desk 11/13/05 07:38:52 PM EST

SYS-CON.TV Webcast: Eclipse IDE For Students, Useful Eclipse Tips and Tricks. Programmers usually work in a so-called Integrated Development Environment (IDE). You can write, compile and run programs there. An IDE also has a Help thingy that describes all elements of the language, and makes it easier to find and fix errors in your programs. While some IDE programs are expensive, there is an excellent free IDE called Eclipse.

Eclipse News Desk 07/20/05 11:29:18 AM EDT

SYS-CON.TV Education Series: Eclipse IDE For Students, Useful Eclipse Tips and Tricks
Programmers usually work in a so-called Integrated Development Environment (IDE). You can write, compile and run programs there. An IDE also has a Help thingy that describes all elements of the language, and makes it easier to find and fix errors in your programs. While some IDE programs are expensive, there is an excellent free IDE called Eclipse.

Eclipse Education 07/20/05 09:07:39 AM EDT

SYS-CON.TV Education Series: Eclipse IDE For Students, Useful Eclipse Tips and Tricks
Programmers usually work in a so-called Integrated Development Environment (IDE). You can write, compile and run programs there. An IDE also has a Help thingy that describes all elements of the language, and makes it easier to find and fix errors in your programs. While some IDE programs are expensive, there is an excellent free IDE called Eclipse.

Gregg Sporar 04/05/05 11:53:46 AM EDT

FYI, NetBeans is also a free, open source Java IDE that is great for students learning Java. The current stable version is 4.0 and 4.1 is in beta. Downloads are available at:

http://www.netbeans.org/downloads/index.html.

Two great tutorials for getting started quickly are at:

http://www.netbeans.org/kb/articles/quickstart-40.html
http://www.netbeans.org/kb/articles/quickstart-webapps-40.html

Tony Austin 04/04/05 08:37:21 AM EDT

Yakov, thanks for the rapid reply!

No, I'm assuming that you already had a suitable JRE and JDK installed. (I already had been using NetBeans, JBuilder and other Java IDEs with these.)

It suddenly hit me what I've been doing wrongly. I have been unzipping the Eclipse platform downloads (such as eclipse-platform-3.0-win32.zip for Eclipse 3.0 or eclipse-platform-M20050311-win32.zip for Eclipse 3.1 M5). When you do this, you get an IDE that it doesn't have the JDT plug-in installed. If you're used most other IDEs (such as NetBeans, Visual Studio) typically install the compiler and other language development support are installed as part of the base installation procedure.

Some novices might not know of course Eclipse is a little different in this regard, being language-neutral. I did know about this, but still made the mistake of unzipping the wrong download.

When I unzipped the SDK download (such as eclipse-SDK-3.0-win32.zip for Eclipse 3.0 or eclipse-SDK-M20050311-win32.zip for Eclipse 3.1 M5) then sure enough the Java support was sitting there ready to be used "right out of the box" -- so everything is goodness if you do it this way.

If I made this simple mistake, then I'm prepared to bet that others will too. Therefore, let me modify my suggestion to recommend that users need to be warned to download and unzip the "SDK" version of Eclipse and not the "platform" version. Fair enough?

Yakov 04/04/05 06:39:44 AM EDT

Tony,

As I've mentioned in the article, installation of Eclipse is as simple as downloading and unzipping of the latest stable milestone or latest release.

Your problems may come from the fact that you do not realize that Eclipse does not come with its own Java Run Time.

To make it clear, if you have a brand new computer do the following:

1. Install Java from any of the vendors, i.e. http://java.sun.com/j2se/1.5.0/download.jsp (at the time of this writing you need to download Update 2).

2. Download and unzip latest stable milestone or latest release from eclipse.org.

Reagrds,
Yakov

Tony Austin 04/04/05 05:41:05 AM EDT

I am not in any way quibbling with the overall content of Yakov Fain's tutorial "Eclipse for Students & Eclipse Tips".

However -- unless I'm completely off track -- I'd say that there's one FATAL OMISSION in it.

I cannot find my downloads of Eclipse Version 2, but to the best of my knowledge for Eclipse V3, it is NOT correct for Yakov to state, immediately after the unzipping the Eclipse download, that the "Installation of Eclipse is complete!"

I cannot recall if it was different for Eclipse V2, but certainly for Eclipse V3 the novice has to learn that at this stage only the base Eclipse V3 has been installed.

As in several other introductory tutorials that I've examined, Yakov failed to explain that before you can work with Java code you have to (a) understand that the Java Development Toolkit (JDT) plug-in is not yet installed; then (b) Learn how to install plug-ins via the Eclipse Update Manager (EUM) via Help / Software Updates / Find and Install / Search for New Features to Install (and so on) -- not a trivial omission.

As one who tried using Eclipse V3 without reading any documentation, I spent quite a few hours completely frustrated, wondering where the Java support was! It wasn't until I stumbled across the plug-in installation process that I realized that the Java support (the JDT) was a plug-in and why Java was not included in the base download.

I DID look for a simple explanation of the process, but could NOT readily find any description of the procedure anywhere in the Eclipse online Help nor on the eclipse.org web site. Maybe it's there somewhere, but it's certainly hard to find! If anything, it should have been one of the FIRST things described in the Help and should be explained in a crystal-clear fashion somewhere on the web site. should it not?

I strongly recommend that the article be updated to incorporate with this critical procedure, otherwise I fear that the target audience (new to Eclipse, and maybe also to Java) will be quickly become as frustrated as I was at the start of my experimentation with Eclipse V3.

Tom Tran 03/25/05 08:21:34 AM EST

In comparing Eclipse and NetBeans, I found that creating a GUI application on NetBeans is easy. On Eclipse, I could not find a way to do it. Please any one point me to a good tutorial.

webmilhouse 03/23/05 04:59:01 AM EST

I don't know if you can fairly compare NetBeans and Eclipse, because Eclipse is more of an IDE platform where you can get scores of plugins related to different languages and tasks. There are plugins for C++, perl, python, PHP, XML, ColdFusion, UML, and a slew of others.

The java perspective in Eclipse is great, and runs much better on my SuSe laptop than NetBeans ever did. I ran NetBeans for a while but switched to Eclipse and never looked back. Plus, I only have to learn the eccentricities of one IDE for all my programming tasks, as opposed to many different ones.

gridlocker 03/23/05 04:56:03 AM EST

So, how is Eclipse compared to Sun's NetBeans?

Answer 03/23/05 04:52:58 AM EST

Most java shops I've seen use IntelliJ Idea. It's the best IDE if you have the cash. Eclipse has a large group of followers among the OSS crowd but, in my experience, SWT's performance leaves a lot to be desired on !Windows environments.

lesson10 03/23/05 03:56:21 AM EST

So how long before Eclipse kills all other (commercial) IDEs?

Dean 03/22/05 04:19:23 PM EST

Check out the following URL about the term "bug":
http://www.agnesscott.edu/lriddle/women/hopper.htm

New to Eclipse 03/21/05 12:09:59 PM EST

Yet another useful article -- thanks Yakov!