Most Read This Week
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
By: Yakov Fain
Jan. 22, 2006 08:30 PM
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.
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.
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:
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:
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 :
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:
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.
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.
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.
Reader Feedback: Page 1 of 2
Subscribe to the World's Most Powerful Newsletters
Today's Top Reads