Flylib.com

Books Software

 
 
 

Using an Integrated Development Environment

   


Using an Integrated Development Environment

In this section, we show you how to compile a program with Eclipse, an integrated development environment that is freely available from http://eclipse.org. Eclipse is written in Java, but because it uses a nonstandard windowing library, it is not quite as portable as Java itself. Nevertheless, versions exist for Linux, Mac OS X, Solaris, and Windows.

After starting Eclipse, select File -> New Project from the menu, then select "Java Project" from the wizard dialog (see Figure 2-2). These screen shots were taken with Eclipse 3.0M8. Don't worry if your version of Eclipse looks slightly different.

Figure 2-2. New Project dialog in Eclipse


Click the " Next " button. Supply the project name "Welcome" and type in the full path name of the directory that contains Welcome.java ; see Figure 2-3. Be sure to uncheck the option labeled "Create project in workspace". Click the "Finish" button.

Figure 2-3. Configuring an Eclipse project


The project is now created. Click on the triangle in the left pane next to the project window to open it, and then click on the triangle next to "Default package". Double-click on Welcome.java . You should now see a window with the program code (see Figure 2-4).

Figure 2-4. Editing a source file with Eclipse


With the right mouse button, click on the project name (Welcome) in the leftmost pane. Select Build Project from the menu that pops up. Your program is compiled. If it compiles successfully, select Run -> Run As -> Java Application. An output window appears at the bottom of the window. The program output is displayed in the output window (see Figure 2-5).

Figure 2-5. Running a program in Eclipse


Locating Compilation Errors

Presumably, this program did not have typos or bugs . (It was only a few lines of code, after all.) Let us suppose, for the sake of argument, that your code occasionally contains a typo (perhaps even a syntax error). Try it out—ruin our file, for example, by changing the capitalization of String as follows :

public static void main(

s

tring[] args)

Now, run the compiler again. You will get an error message that complains about an unknown string type (see Figure 2-6). Simply click on the error message. The cursor moves to the matching line in the edit window, where you can correct your error. This behavior allows you to fix your errors quickly.

Figure 2-6. Error messages in Eclipse


These instructions should give you a taste of working in an integrated environment. We discuss the Eclipse debugger in Chapter 11.


   
top
   


Compiling and Running Programs from a Text Editor

An integrated development environment accords many comforts but also insinuates some drawbacks. In particular, for simple programs that are not distributed over multiple source files, an environment with its long startup time and many bells and whistles can seem like overkill. Fortunately, many text editors can launch the Java compiler, launch Java programs, and capture error messages and program output. In this section, we look at Emacs as a typical example.

NOTE

GNU Emacs is available from http://www.gnu.org/software/emacs/. For the Windows port of GNU Emacs, see http://www.gnu.org/software/emacs/ windows /ntemacs.html. Be sure to install the JDEE (Java Development Environnment for Emacs) when using Emacs for Java programming. You can download it from http://jdee.sunsite.dk. You need JDEE 2.4.3beta1 or later with JDK 5.0.


Emacs is a splendid text editor that is freely available for UNIX, Linux, Windows, and Mac OS X. However, many Windows programmers find the learning curve rather steep. For those programmers, we can recommend TextPad. Unlike Emacs, TextPad conforms to standard Windows conventions. TextPad is available at http://www.textpad.com. Note that TextPad is shareware. You are expected to pay for it if you use it beyond a trial period. (We have no relationship with the vendor, except as satisfied users of the program.)

Another popular choice is JEdit, a very nice editor written in Java, freely available from http://jedit.org. Whether you use Emacs, TextPad, JEdit , or another editor, the concepts are the same. When you are done editing the source code, you invoke a command to compile the code. The editor launches the compiler and captures the error messages. You fix the errors, compile again, and invoke another command to run your program.

Figure 2-7 shows the Emacs editor compiling a Java program. (Choose JDE -> Compile from the menu to run the compiler.)

Figure 2-7. Compiling a program with Emacs


The error messages show up in the lower half of the window. When you move the cursor on an error message and press the ENTER key, the cursor moves to the corresponding source line.

Once all errors are fixed, you can run the program by choosing JDE -> Run App from the menu. The output shows up inside an editor window (see Figure 2-8).

Figure 2-8. Running a program from within Emacs



   
top