Compiling and Running Java Code with jGrasp

This guide assumes that you already have jGrasp installed, which is preinstalled on the lab machines. jGrasp can be downloaded here (look for the “Download” link on the top left; you can skip the survey and scroll straight to the bottom).

Step 1: Launch jGrasp

Exactly how you do this depends on your particular configuration.

Step 2: Create a New Java Source Filke

You can create a new Java source file by selecting File -> New -> Java. An illustrative image is provided below.

Step 3: Write Your Code

When you ask jGrasp to create a new file, an embedded window will appear wherein you can write and edit your code. An illustrative image is provided below.

Step 4: Save Your Code to a File

While you have been working with a file in jGrasp, it has not yet been saved to the computer. You will need to save the file explicitly before you can actually compile or run your code. You can save the file by selecting File -> Save from the menu, as shown in the image below.

Selecting Save will allow you to choose exactly where you want to save the file, along with what the filename should be. In Java, the filename must always match the name of the class you have created in the file. For example, if the class name is Foo, then the code must be saved in a file named Foo.java. While you can save your code with a different filename, you won't actually be able to compile and run your code later. Assuming your code contains a class definition (e.g., public class Foo), jGrasp will automatically choose a correct filename (in this case, Foo.java).

Once you have selected a place to save the file along with the filename, hit the Save button. An illustrative image is shown below.

Step 5: Compile Your Code

Once your code is written, you can try to compile it. Internally, jGrasp performs this by running javac, though it hides this detail from you. You can tell jGrasp to compile your code by selecting Build -> Compile, as shown in the image below.

Note that compilation won't necessarily succeed. For example, the compiler will reject your code if it has a syntax error, which includes things like forgetting to match a left brace ({) with a right brace (}). If compilation doesn't succeed, error messages will be shown in the window at the bottom, illustrated in the image below. In the case of the image below, there were no errors; it shows that javac was run and subsequently exited normally. Because javac was successful, a class file was produced, namely HelloWorld.class. The image below also shows that HelloWorld.class has appeared in the same folder/directory as HelloWorld.java.

Step 6: Run Your Code

If your code compiled successfully in the previous step, then you'll be able to run it. You can run your code by selecting Build -> Run, as illustrated in the image below.

You can see the output of your code in the window at the bottom, in the same location you looked for compiler errors in the previous step. This is shown in the image below. Be sure to look at the output to make sure your code produced what was expected; oftentimes we don't get it right the first time!