This week we are going to show you how to use jGRASP, a Java IDE (Integrated Development Environment) developed at Auburn University. Because jGRASP itself is written in Java, it can run on any platform that supports Java. That's how we are able to use the same IDE with both Linux and Windows.
We will only describe a few features of jGRASP in CSCI 201. To become a jGRASP guru, you should consult the draft jGRASP Handbook which may be found on the CD-ROM bundled with your textbook or downloaded from the GRASP project home page.
Windows users may start jGRASP via the usual start button. Linux users can start a terminal session and then type the command "jgrasp". Linux users may also configure GNOME to start jGRASP through the menu system. We won't discuss that here.
In Windows, jGRASP will bring up two different windows.
Usually, the first is a jGRASP Control Shell.
You can probably ignore it unless jGRASP
is "hung". In that case, you may want to raise this window
and press the button labeled "Shut Down jGRASP, Forcibly if
Necessary".
The real jGRASP window usually covers up the
jGRASP Control Shell.
By default, it starts running the Windows
look and feel.
Because we want to use the Metal (Java Swing) look and feel in our examples, go through the Settings ⇒ Look And Feel ⇒ Metal selections of the menu bar to achieve Metal.
Linux jGRASP also starts with two initial windows.
The first
is a small window that is actually displayed by a small C program
used to control jGRASP. You can ignore this window unless
you need to terminate jGRASP.
The second window gives an ominous disclaimer about "bugs".
Just press the Don't Show Again button and
go about your business.
Soon the Metal interface will be displayed.
Windows or Linux, you ought to see something like this:
jGRASP, like many IDE's, uses projects to organize programming tasks. A project is a collection of related programs used to solve a problem. Although you really don't need projects in your early CSCI 201 assignments, you might as well get into the habit of using them.
In the Windows labs, we'll be storing our projects in subdirectories of the C:\Files directory. These projects will be lost whenever you reboot one of the UNCA lab computers, so you probably want to know how to transfer these to a floppy disk.
In the Linux world, projects will be stored in subdirectories of your personal csci/201 directory. Although these projects will persist between Linux login's, you need to know how to transfer these projects to a floppy if you want to take them home.
First follow the menu bar choices Projects ⇒ New Project ... to bring up the New Project dialog. This dialog is built with the Java Swing file search interface. There are two boxes near the words "Look In". The box to the right can be used to move down the directory hierarchy (toward the root) or to move to different drives. The much larger box, which is below Look In, is used to move into subdirectories.
Unix | Windows |
---|---|
Linux users should now head toward csci/201; and Windows users, toward C:\Files. If you are having trouble following the route, you might check out our Extremely Tedious and Detailed Guide to jGRASP Directory Search.
In our labs, we're going to create a new subdirectory for each project. We recommend you also follow this practice in your programming assignments. To do this, press on the icon that vaguely looks like a file folder, . When you pass your mouse over this icon, the words "Create New Folder" should appear.
A new folder called New Folder will appear. Rename this new folder Lab2a. This maneuver can be difficult for even the most experienced Windows or Linux user. You must gently and patiently click the words "New Folder" until they are highlighted. Then you can edit the file name. If you are getting very frustrated in your renaming, check out our Extremely Tedious and Detailed Guide to jGRASP Directory Creation.
Double click on your newly created Lab2a to move in. Finally, type the word "Lincoln" into the text box labeled File Name and click the button labeled Create. A new project named Lincoln.gpj will be created.
If you look in the left pane with the Browse tab selected, you should see something resembling the following:
Unix | Windows |
---|---|
This is your first checkoff point. Show your lab instructor your jGRASP window, so that he/she can verify that you have created the project in the right directory.
Yes, it's finally time to actually create a Java file in jGRASP. First you must create an new CSD, or Control Structure Diagram, window for the code. Do this from the menu bar as File ⇒ New File ⇒ Java.
The new CSD window will appear in the right pane. Notice that the CSD window has its own title bar and menu bar. Note that menu bars of both the main jGRASP window and the CSD jGRASP window have a choice labeled File. This can get confusing.
You'll probably want the CSD window to fill the right pane. Mash the CSD's Maximize icon, , to make this happen.
Cut and paste the following familiar Java program into the CSD window using the CSD's Edit ⇒ Paste.
public class Lincoln { public static void main (String[] args) { System.out.println ("A quote by Abraham Lincoln:"); System.out.println ("Whatever you are, be a good one."); } }
Using the CSD's File ⇒ Save As ... menu choices,
save your Java code in the file Lincoln.java within the
Lab2a directory.
If you are using Windows, you should save your file in "Binary/Unix" format. If you don't you will discover that, when you reload your Java program into jGRASP, the two-letter "word" OD will appear at the end of each line. This is actually, 0D (zero-D) the hexadecimal representation of the enter key. You may consider this a jGRASP bug. |
Follow the CSD's File ⇒ Add to Project ⇒ Relative Path trail to actually add your Java code to the project. If you press the Project tab of the right pane, you should now see your Java program associated with the jGRASP project. |
At last it's time to generate some bytecode. You have three choices for doing this.
The bottom panel of jGRASP should display a message that indicates successful compilation of your program. If it doesn't, you need to get the attention of your lab instructor.
You have three ways to run your program.
The bottom panel of jGRASP should automatically select the Run I/O tab and display the output of your program.
Show your lab instructor the result of running your program.
Use the "main" jGRASP window to terminate jGRASP. Linux users may also have to terminate the smaller jGRASP control window.
We all know floppy disks are a somewhat dated way to transfer data, but they'll do in a pinch.
Bring up the file explorer. If you don't remember how, you can always review CSCI 201 Lab 1.
Once file explorer is running, navigate through the left pane
to the directory C:\Files. The contents of
C:\Files should then
be displayed in the right pane. Now press on the icon for
Lab2a and drag it over to the A:.
Double-click on A:\ to make sure the move was successful.
First of all, it is recommended that you format your floppy disk on a PC running Windows before using it on the Linux computer. After you place a floppy disk into your Linux computer, the root directory of the floppy disk will appear as the directory /mnt/floppy as soon as you mount it. Then you can use the cp -R, copy recursively, command to move all the files of a specified directory to the floppy disk. Next, use ls -R, list recursively, to view the entire contents of the floppy disk. Finally, unmount your disk. Here are the commands for performing the transfer to floppy disk. Notice that the last command is "umount", not the more natural "unmount".
[XXXX@YYYY ZZZZ]$ mount /mnt/floppy [XXXX@YYYY ZZZZ]$ cp -R csci/201/Lab2a /mnt/floppy [XXXX@YYYY ZZZZ]$ ls -R /mnt/floppy [XXXX@YYYY ZZZZ]$ umount /mnt/floppy
Show your lab instructor that you have copied your project directory to your disk.
In this course you're must frequently download collections of files for use in labs and assignments. We're going to use jar, or Java archive, files for this purpose in CSCI 201.
In your browser click on the following link for the Java archive for part B of Lab 2. We recommend that Windows users save the file in C:\Files; and Linux users, in csci/201.
Restart jGRASP. Use the Project ⇒ Jar / Zip Extractor ... menu choices to bring up the jar extractor dialog.
Now use the File ⇒
Open Jar or Zip File ...
menu path of the jar extractor dialog to bring up an
Open File dialog.
Navigate to the directory where you copied the jar file,
select it, and open it.
Now the jar extractor dialog shows the files that may be extracted.
Take the File ⇒
Extract Files ...
menu choice to bring up an Extract to Directory dialog box.
Create the new directory Lab2b for your files and
press Extract. Your files should be waiting as soon
as you exit this dialog.
Use the Project ⇒
Open Project ...
menu choices to open project Lab2b .
Select the Project tab of the left pane and then
double-click on the TryIt.java entry.
A CSD windows containing Java code should appear in the
right pane. You'll probably want to maximize the CSD window.
Ignore the jGRASP message that your files may have been
edited outside of the control of jGRASP.
Show your lab instructor that you have extracted your project into the right directory.
Yes, there are a lot of checkoff points in this lab. We're just trying to make sure you get started on the right track.
Now compile the program. (Remember, either or control-B.)
Oops. There's a error in our program. jGRASP is very helpful here.
It highlights the Java compiler messages in green and tries to
display the offensive Java statement on a black background.
Fix the program by adding a semi-colon to the end of the bad line of Java. Re-compile.
Run the program. (It's or control-R.)
This time the bottom pane displays a run-time error:
java.lang.NoSuchMethodError: main Exception in thread "main"
The problem here is that the main method is called Main, rather than main. Fix, re-compile, and re-execute.
Sunday Monday Today is Tuesday Wednesday Thursday Friday Saturday
So you ran the program and saw the output. Wouldn't it be more fun to see the Java code be executed a line at a time. This is something we can do with the debugger. First, we must recompile the program in debug mode. Use the Compile ⇒ Debug Mode menu path to enable debugging. Then re-compile your program. |
Now you must set a breakpoint in your program to interrupt normal program execution. Do this by clicking your cursor anywhere within the line that says:
Next right click within the CSD window. You should see a new pulldown menu. Select Toggle Breakpoint. A right circle should appear to the right of the selected line. Be careful not to select Toggle Bookmark. |
Press the debug icon, , or use the Run ⇒ Debug menu choice, to start executing your program with the Java debugger. You program should immediately halt.
Make sure the Debug tab is selected in the right pane. You should see four separate displays in this pane.
The first is a row of debugging control icons. Move your mouse slowly across this row to get a brief description of what each does. We'll only use the leftmost icon, the downward pointing arrow, in this lab. Each time it is clicked, one statement of your program will be executed. Go ahead and click it a few times. |
|
The next is a display of threads. Java can use thread to simultaneously execute several methods. This is important in networking and animation applications and you may learn to write multi-threaded Java code in upper-level CSCI courses. |
|
The third display is the call stack. Right now you see that
the |
|
You'll notice that the final display has two tabs. Presently you
should be on the variables tab. This display will
probably be the most helpful to you because it shows you the values
of the variables being used by your class and method.
Step through your program and see how the value of |
Show your lab instructor that you can step through your program
Go home.