COMP 14: Introduction to Programming. Projects.

Projects with Multiple Files in jGRASP

This information has been collected from the jGRASP tutorial.
Projects are explained in Chapter 4 in the tutorial.
Creating a .jar archive file is explained in Section 4.5 in the tutorial.
Please read Chapter 4 before starting your next homework assignment.

Why a Project?

A typical Java program consists of several classes. If a program becomes big (measured in terms of lines of code), it is common to separate the code into different files.
In Comp14 we will follow the procedure of storing each class in an individual file.

Files can be grouped together in a project. This allows you to establish a relationship between files, create archive files (same idea as .zip files), etc.

Archive files make it easier to submit homework assignments with multiple files.

Creating a Project

If you open jGRASP, you will notice that the top bar has the message [default] - jGRASP. 'default' refers to the default project.
In order to create a new project, click on Project, and scroll down to New Project...

You will be required to select the directory where you want to store the project. Browse to the appropriate directory, and enter the name of the project.
Let's consider an example project: RectangleTesterProject. When you click Create, a file RectangleTesterProject.gpv will be created in the directory you selected.

The name of your project can be selected, for example, adding the word Project to the name of the class that containst the main method.

Once you have created the project, you will see that the name in the top bar reflects the name that you selected.
Also, you can easily access the different files in the project through the Project window. To display this window, click on the Project tab towards the left bottom in jGRASP.

Adding Files to a Project

Now it is time to create your classes in separate .java files and add them to the project.

It is convenient to store all the .java files in the same directory as the project file (.gpv). This will ensure that the different classes are correctly located during compilation.
It is also convenient to store all the files related to a project in one independent directory, and don't mix them with files from other projects.

If you want to add a new file to the project, create the file as usual, File/New File/Java.
When you save the file, select as the directory the same one where the .gpv file is located.
For example, let's create a class Rectangle, which we will save in the file Rectangle.java. We will save this file in the same directory as RectangleTesterProject.gpj.

Once you create the file, you can add it to the project.
Click on Project at the top bar, and scroll down to Add Files To Project...
Pick the desired file (Rectangle.java), and then click on Add.
Note that nothing seems to happen after clicking Add.
However, the file has already been added, and you can test it by browsing to a different directory and coming back to the same one.
You will notice that a symbol [P] has been appended to the file that was added.

In order to exit this menu, click Cancel after you have added all the files you wanted.

You can add all the files at once, one by one, or in groups.

Following a similar procedure, you can also remove files from your project.

Compiling and Executing

It is advisable to have only one class with a main method in each project.
Given the reduced size of the projects that we are going to create, please follow this guideline.

You can compile all the files in your project 1 by 1, or you can directly compile the file whose class containst the main method.
jGRASP identifies that this file depends on other files, and it will compile those automatically for you.

.java files without a main method cannot be executed. In order to run a program, you need to activate the window with the file that contains the main method, and then you can run the program.

You can run the debugger on a program that uses multiple classes.
If you try to step inside a method of a class that is stored in a separate file, jGRASP will automatically open that file for you and go to the appropriate method.

Creating a Java Archive

Java archive files (.jar) allow you to store several files together in a single archive file.
This is very handy to submit homework assignments.

To create the .jar file, make sure that the project is open.
Then, click on Project, and scroll down to Create Jar File for Project...
A window will pop up.
Select the boxes for Project File and Sources.
Then click on Next.
A new window will pop up.
After determining the directory and name for the .jar file, select Create Jar.

You can test if the .jar file was created properly by opening it.
Before that, close your project and all files.
Copy the .jar file to some other directory, and then select Project and scroll down to Jar / Zip Extractor.
In the next window, select File \ Open Jar or Zip File and locate the .jar file.
Select the file and open it.
The list of files inside the .jar file will appear.
Now click on File \ Extact Files and select the directory where you want to store the files.

Before submitting a .jar file as the result of your homework, copy it to a separate directory, extract it inside jGRASP, and make sure that you can compile all the .java files and that you can run the program properly. Then it is time to submit the assignment.