February 17, 2019
Hot Topics:

Java Development with Eclipse

  • August 19, 2005
  • By Peter Nehrer
  • Send Email »
  • More Articles »

Editors may choose to use the Content Outline view to present a simplified structure of the file being edited, such as the structure of your Java file with its classes, fields, and methods. Thus, this view is highly editor-specific.

Bookmarks, Tasks, and Problems show elements of the Resource model commonly referred to as Markers. In short, the application may choose to annotate certain resources with these markers; for example, you can create a bookmark at a specific line in a file, so you can get back directly to it later; you can create a task related to some Java code (for example, log exceptions in a catch block); and lastly, the Java editor may annotate your source file with problem markers, denoting compilation errors or warnings. These are then summarized in their corresponding views.

The Properties view is a universal kind of view, which shows select properties exposed by the currently selected object somewhere in the workspace (be it an editor or another view). Exactly what properties are shown depends on the application.

As I previously mentioned, results of a search invoked through the Search dialog are automatically displayed in the Search Results view. This view lets you filter and organize the results, as well as open an editor at the denoted location. The view remembers results of several previous searches.

The Console shows the output of programs and external tools invoked through Eclipse. You'll see this view in action when you launch the simple program you'll develop using JDT.

Lastly, the Progress view shows the progress of long-running operations, such as application builds, searches, and so on. It is related to Eclipse concurrency support—whenever a progress dialog is displayed as a result of some user action, the user may click "Run in Background" button, which will close the dialog and allow the user to continue with another activity (rather than blocking the whole application). Results of operations running in background can be tracked in the Progress view.

Feel free to continue exploring the menus, dialogs, and views on your own. When doing so, remember that because the main application menu and toolbar are shared with the currently active editor, you will likely find additional functionality as you edit different types of files.

Here Comes Java

The JDT plug-ins build nicely upon the foundation described above. In fact, the JDT team provides significant input into the Eclipse Platform itself, thus ensuring that the various abstractions embodied in the Eclipse Platform are actually useful to a "tool vendor" such as JDT. If you switch back to your Java perspective and re-examine the menu bar, you will find several additional menus, as well as menu items added to the standard menus. To provide a comprehensive guide to Eclipse's Java Development Tools would require a rather thick book. (I do recommend some in the Resources section at the end of this article.) What I'll do instead is rely on your intuition as a developer, as well as your experience with other IDEs, and only take you through some basic steps and concepts necessary to get started with developing Java applications (and ultimately, Eclipse plug-ins) on your own. To help you along the way, you'll develop a single-class application, which will remind many of you of your first program that you ever wrote, be it in Basic or Pascal or whatever else was hot at the time.

First, create a simple Java project (make sure you are in your Java perspective):

  1. Click File -> New -> Project.
  2. In the New Project wizard, select Java Project and click Next.
  3. Enter sandbox as the Project's name, check Create separate source and output folders, and click Finish.

What you should see now is your new Java project appearing in the Package Explorer view. Look at its Project properties—right-click it and choose Properties (at the bottom of the context menu). This will bring up a Properties dialog, with the Project's properties grouped in categories on the left-hand side. Some of the common property pages (such as those under Info and Builders) are not Java-specific, others are. Look at Java Build Path. This set of properties greatly influences how your Java project will behave in the workspace.

The Source tab shows a list of Source folders on your project's build path. These are specially designated folders that contain your Java source files (in their respective package hierarchies). Even though you may place Java source files outside of your Source folders, those will not compile (and will not be converted into executable byte-code). Note that you may configure more than one Source folder per project, but typically all of them produce output into a single Output folder. In special circumstances, you can choose to have a separate output folder per each source folder, as well as specify source folder inclusion/exclusion filters.

The next tab is labeled Projects. Currently, it's empty, but in a multi-project workspace, it would show other Java projects, whose exported code may be placed on this project's classpath (and thus, your classes could reference that project's code).

The Libraries tab shows your project's library dependencies—local or external JARs, libraries, and class folders (containing compiled *.class files). In this project, the only library required is the JRE System Library.

The Order and Export tab enables you to specify which of your source folders will be exported, and which of your dependencies will be re-exported. To "export" something in this context means to make it visible to other projects that reference this project in their respective properties page (the Projects tab). In your case, you'll leave the source folder exported (although no other project in this workspace will need it).

Other property pages, such as Java Code Style or Java Compiler, enable you to override your workspace-wide Java preferences at the project level.

Close the Properties dialog and right-click the src folder in your Package Explorer. Choose New -> Class. This will bring up a New Java Class dialog. Note that you could have invoked this dialog by choosing File -> New Class from the main application menu (and there are several other ways to do that). Type com.developer.sandbox in the Package field, and Main in the Name field. Click Finish. This will create a new, empty class in the specified package and open it in the Java Source Editor. Notice how the Outline view now reflects the structure of your Java file.

Editing Java Source Code

The Eclipse Java Editor provides many productivity maximizing functions that you'd expect from a top-of-the-line Java IDE, such as syntax coloring, code formatting, templates, folding, info pop-ups on hover, and so on. When writing your application, you'll take a brief look at two very useful features—the Content Assistant and Refactoring support.

The next step in your development quest is to create the main method that will represent the core of your mini-application. Place the cursor in the body of the Main class, type main and press Ctrl+Space. You've just activated the Java Content Assistant. The top entry in the displayed list box is the main template. Templates are configurable, named snippets of text that can help you with some repetitive patterns of Java code. Choose main - main method and press Enter. It'll generate an empty main method and place the cursor inside its body.

Your code will consist of an infinite loop, the first part of which will prompt the user for some numeric input, and the second part will echo it back (clever, ain't it?). So, start by instantiating an InputStreamReader to read from System.in: Type new, press Ctrl+Space and choose the new - create new object template. Enter InputStreamReader in the first box (in other words, the first template argument), then press the Tab key and enter in as the variable name. Press the Tab key again and enter System.in as the constructor argument. Press Enter. The code looks fine, except you'll notice a wavy red line under both instances of InputStreamReader. You'll also notice a red error marker in the editor's left margin. Behind the marker is a lightbulb, indicating that content assistance is available—click it and choose Import InputStreamReader (java.io). This will import the unresolved class and the error marker will go away (it'll actually be replaced by a yellow warning marker, indicating that the new local variable is not used anywhere—that's okay for now). Programming in Java can be that easy!

Finish the method implementation by adding the following snippet of code:

BufferedReader reader = new BufferedReader(in);

while (true) {

   System.out.print("Enter a number: ");

   String input = reader.readLine();

   System.out.println("You entered: " + Integer.parseInt(input));


You can press Ctrl+Shift+O to import all unresolved types at the end. Notice that as you type some method invocations, for example, System.out.print, the Content Assistant continues to present you with options, such as members of the object whose method you want to invoke (for example, System.out.—note the dot—will bring up a list of members of PrintStream; typing print will narrow the list down to those starting with "print").

There is one last error to be resolved—apparently, reader.readLine() throws an IOException (if you save the file now and activate the Problems view, you'll see the error listed there). Click the lightbulb in the editor's left margin (or press Ctrl+1, which is another way to activate the Content Assistant) and choose Surround with try/catch. This will wrap the offending line in a try/catch block, taking the variable declaration out of it and providing a default catch block, which simply prints the exception's stack trace. But this creates another problem—the local variable input may not have been initialized. This will happen if an exception is thrown. In this case, the Content Assistant will recommend initializing the variable.

While you're at it, note the // TODO Auto-generated catch block line in the catch block. If you activate the Tasks view, you'll notice a task with precisely that description; for every single-line comment starting with TODO, the editor creates a task (this is, by the way, also configurable).

Look at some code refactoring support provided by JDT. Select the lines that prompt the user for input, starting with the first System.out.print call and ending with the catch clause. Right-click the selection and choose Refactor -> Extract Method... Enter prompt as the method name and click OK. You'll see that the entire block of code was moved off to another method, with a signature that allows the reader instance to be passed in. The method's invocation is now in place of the extracted code.

More refactoring functions are available, depending on your context (for example, some apply to classes, some to interfaces, some to methods, and so on.) New ones are usually added with each new release. In addition to refactoring, you can find some useful source code manipulation functions under the Source sub-menu of the Java editor's context menu (or in the main menu bar). For example, you can generate getters/setters for your fields, create constructors based on fields, generate delegate methods, and more.

Launching Java Applications

Now that you are finished with the code, you can test-run the application to see how it works. From the main menu, choose Run -> Run As -> Java Application. You can also right-click the Main class in Package Explorer and choose Run As -> Java Application, if you prefer. You should see the Console view at the bottom of your screen become active, with the familiar prompt asking you to enter a number. Before entering your input, make sure to click the Console view so it has the input focus. To terminate the program, press Ctrl+C, or hit the red-square stop button in the Console view.

The Console view is typically customized for the type of launch configuration whose output it displays. For example, output of Java applications is annotated in such a way that exception stack traces contain clickable links to the referenced source code. To see this in action, enter a word (say, blah) at the program's prompt instead of a valid integer (if you already terminated it, you can quickly re-launch the last run configuration by pressing Ctrl+F11). What you should see is an exception stack trace, caused by a NumberFormatException resulting from an attempt to parse the word "blah" as a number. At the bottom of the stack trace you'll see the offending line in our Main class, underlined. Click the link, and you'll be taken to the specified line in the editor.

Eclipse provides broad support for launching and debugging code of any kind, be it Java or another language. The necessary launch configuration information is stored in your workspace; to examine what was created for this program, click Run -> Run... This dialog will show all launch configurations in your workspace, grouped by category. You can examine all properties related to this program's configuration, such as the main class, program arguments, runtime classpath, and so on. Typically, you can launch the same configuration in debug mode; instead of choosing Run..., choose Debug... in the Run menu.


In this article, you took a brief tour of the Eclipse IDE Workbench and its standard menus and views. You also learned about workspaces and the Resource model used by many Eclipse-based tools. While developing a simple Java application, you examined some useful JDT features, such as the Content Assistant and Java Refactoring support. You also saw a little bit of Eclipse's Launch framework at work. In the next installment of this series, you will take a look at the Plug-in Development Environment (PDE) and create your first Eclipse plug-in.


About the Author

Peter Nehrer is an independent software consultant living in Toronto, Ontario. He specializes in Eclipse-based enterprise solutions and J2EE applications. His professional interests include development tools, model-driven software development, and information sharing. He is the author and contributor to several Eclipse-related Open Source projects. Peter is an IBM Certified Solutons Developer for XML and related technologies. He holds an M.S. in Computer Science from the University of Massachusetts at Amherst, MA.

Page 3 of 3

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date