Compiling and Running a Program with SDK


Translating Your Program

When your source program is entered and saved, it is time to translate it into the computer's machine language. In xemacs, this can be done using the editor compile command M-x compile (press and release the Meta, Escape, or Edit key, then press the x key, and then type the word compile) at which point xemacs will echo what you type in the mini-buffer. This will shift the cursor to the mini-buffer (at the bottom of the window) where xemacs will print

   Compile command: make -k

For simple programs like this one, the easiest way to translate our program is to replace the make -k with an explicit call to the Java compiler javac. Using the delete (or backspace) key, erase the make -k and replace it as follows:

   Compile command: javac Mult.java

This changes the compile command from a call to make (a program we will learn about in a few weeks) to an explicit call to the Java compiler javac, asking it to translate the program in the file Mult.java.

Once you have edited the compile command, press the Return key and xemacs will split its window in two, displaying the Mult.java buffer (containing your source program) in one half and displaying a new *compilation* buffer (containing the results of your compile command) in the other half. If Mult.java is free of errors, then you should see a message like:

   Compilation finished at Mon Sep 5 9:12:44

in the *compilation* buffer. If you instead see an error listing then you have made typing errors in entering the program. Compare the program above with what you have written and then change what you have written to fix the error. Then use the compile command again to see if you fixed the mistake. Repeat this edit-compile cycle as many times as necessary, until your program compiles correctly. Once your program compiles correctly, javac writes the resulting byte code file to the working directory, giving us this picture:

Use ls to verify that your directory contains the executable file named Mult.class (it may also contain additional files, such as automatic back-up files created by xemacs).

 

Running Your Program from the Command-Line.

Running program Mult requires us to use the Java run time interpreter. Fortunately, this is not too difficult. We must enter Java followed by the name of the class of our program.

If you are using the X-Window environment, the xemacs window is just one of many independently running windows. Locate a different window in which the system prompt is displayed (i.e., the xterm window from which you invoked xemacs). Simply move the mouse into this window (or perhaps click in it) and the cursor should appear, indicating that it is ready for your command.

If you are using a terminal or telnet instead of the X-Window system, use the xemacs suspend command: C-z. The operating system will then suspend xemacs and display the system prompt, indicating it is ready to accept a command.

Once you are at the operating system prompt, use pwd to check that your working directory is your 0 subdirectory. (If not, change directory to make it so.) Then use ls to display the contents of 0, to make sure that both the source file Mult.java and the byte code file Mult.class are there. At that point, you can enter

   % java Mult

and your program should execute successfully. If it does not, ask your instructor for help.

 

Returning from the Command-line to Xemacs

If you are using the X-Window environment, simply move the mouse from your xterm window back into the emacs window. (If necessary, click the mouse.)

If you are using a terminal or telnet and you suspended xemacs using C-z, then enter:

   % fg

which changes xemacs from being suspended to being a foreground process.  

 

Printing a Hard Copy of Your Program

A paper copy of electronic information is called a hard copy. It is often useful to have a hard copy of one's source program, which is in the file Mult.java.

In an AT&T UNIX command-line environment, you can usually print a hard copy of this file by entering

   % lp Mult.java

or

   % lp -d PrinterName Mult.java

while in the Berkeley UNIX command-line environment, you can usually print a hard copy by entering

   % lpr Mult.java

or

   % lpr -PPrinterName Mult.java

Your instructor will inform you of the details of printing at your institution.

 

Quitting xemacs

To quit xemacs (as opposed to suspending it), you can type the editor quit command: C-x C-c (Cntrl and x, followed by Cntrl and c).

 

Printing an Execution Trace

It is often useful to be able to print a hard copy of the output from your program, especially for more complicated programs. To do this use

   % java Mult | tee -a trace

This redirects the output into a file named trace. All of your output should now be in the new file which you can print as normal.

 


Back to the Lab


Back to the Table of Contents

Back to the Introduction


Copyright 2000 by Prentice Hall. All rights reserved.