Java Tips

Object-Oriented Programming

This page contains some practical pointers on the use of Java. I'll add more tips to the page as people ask questions and as time permits. So, if you do something cool that you think others in the class might like to do, please pass on the information and I'll post it here. All senders of tips will receive credit in the form of a mention here (and perhaps in the form of gratitude from present and future 062 students, too).

Feel free to browse the whole page, or use this index to jump to a particular topic:

If you run into any problems with these tips or using Java, please let me know.

Making a Grasp Project for an Existing Java Program

This is what I did to create the project file so that it would compile and run the application under JGrasp:

  1. Make sure all of the Java files are in the same folder.
  2. Start JGrasp.
  3. Select 'Project' >> 'New Project'. This opens up a window in which you can find and Add the files to a new project. The result is a new project file.

You may now need to modify the project settings in order to compile: and run the application under JGrasp:

  1. Select 'Settings' >> 'CSD Window Settings' >> 'Project'
  2. Click on the Languages tab, and then click on the Flags/Args/Main tab.
  3. Make sure there is nothing in the Main File section. If there is, then delete it.
  4. Click on the boxes to the right of Compile and Run sections.
  5. Enter the names of all the files in the project, separated by spaces, not commas. For example, for the Lunar Lander project, you might enter the following:
             -- Put this info in the Compile section:
             -- Put this info in the Run section:

    The only difference between the two is that you include the class with the main() method in the Run section, because you need that method in order to run the program, but you leave it out of the Compile section. Including it there causes a compiler error by trying to compile that class twice. Pretty messed up if you ask me. [Editor's note: I agree.]

(Thanks to Chris Simonis.)

Running Java Command-Line Applications in Grasp

Once you have compiled your program successfully...

(Thanks to Bill Learn.)

Capturing Standard Output from the Grasp Run I/O Window

Under the Run menu in jGrasp, choose the "run in MSDos window" option. When the program is running, you will see a little C:\ icon in the top left corner of the window. Click on that, and a menu will pop up. Go to edit, and then to mark. You can highlight the text in the MSDos window with this. Then go back to edit, and then to copy.

(Thanks to Sarah Miller.)

Compiling Java Programs in Grasp

Here is how to set up your Grasp environment to compile Java:

The rest of the instructions assume that the JDK is installed on your machine. For example, you might install the JDK 1.1.8 in a folder of the same name on your C drive. A standard installation might result in a CLASSPATH variable of


and a PATH variable of


Now let's return to the set-up:

Now, if you save your programs to the \bin folder, you be able to do semantic checks and compile your Java programs. [There may be another option for setting the location of the program to compile. If you know how, please let me know!]

(Thanks to Bill Learn.)

Running Java Graphics on a Remote Server

Many of you prefer to work on one of the PCs in Wright Hall to edit Java programs. You can then compile and run these programs on chaos. This approach works fine for standard IO-based applications, since the Java application can write directly to the terminal.

When we move into graphics-based programs, though, we encounter a problem. Chaos needs to know how to display the windows created by your program on the computer you are using.

Here is how you can tell chaos to display to a PC in the lab:

This creates a telnet session to chaos with the session set automatically to display graphic output to the PC. You can, of course, use the same telnet session to edit and compile your programs.

If you are at home and have an X-windoes client such as xwin32, then you can make the connection between your PC and chaos by typing this command at the chaos prmpt:

setenv DISPLAY

I do not yet have a complete answer for how to do the same thing from your home PC, unless you also have an X-windows emulator installed there. Once I hear how to do it in another way, or that it can't be done in any other way, I'll let you know.

(Thanks to Matt Behrens.)

Making Another Disk the "Home" for Java Programs in Grasp

Some students prefer to save, compile, and run their Java programs on a Zip disk or a floppy disk when working at home, so that they don't have to move files all the time. To do this, you need to tell Java to look the right places at the right times. Try using ".;" at the front of your CLASSPATH variable under Grasp. For a standard installation, this would give something like:


Adding the "." to the front of your path tells Grasp and Java to look in the current directory first for the files to work with.

You do not need to change your PATH variable.

(Thanks to Stew Smith.)

Compiling Java in Linux

Some students have recommended a Java compiler for Linux available from IBM AlphaWorks. At that site, you can download a tarred file. Just untar it, update your path, and you're up and running.

(Thanks to Austin Lorenzen.)

Eugene Wallingford ..... ..... January 12, 2004