Running the first program
Open a file in jGRASP, compile it, run it. Ten minutes, and then every other week works.
Using jGRASP
-
Unzip the download
Double-click
week-0.zip. A folder appears withHelloWorld.javain it. Keep all Java files in folders like this one, not loose on the desktop. -
Open the file
Open jGRASP, then File → Open and pick
HelloWorld.java. The code shows up in the editor in the middle. -
Compile it
Build → Compile. This turns the code into something the computer can run. If nothing happens, that is good news. It worked.
-
Run it
Build → Run. The bottom pane switches to Run I/O and prints
Hello, world! -
Type into it
Later programs ask questions. Answer by clicking the Run I/O pane at the bottom, typing, and pressing Enter. That pane is where the program's input and output both live.
jGRASP compiles what is saved on disk, not what is on screen. If code changes and compiles without saving, the old version runs. Save first, every time.
When it does not compile
Red text at the bottom is normal. Everyone gets it constantly.
- The messages appear in the Compile Messages tab at the bottom.
- Double-click a message and jGRASP jumps to the line it is complaining about.
- Fix the first error and recompile. One mistake often causes five messages, and fixing the first clears the rest.
The file name has to match the class name exactly, including capitals. HelloWorld.java holds public class HelloWorld. Rename one, rename the other.
What the code says
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
| Line | What it does |
|---|---|
public class HelloWorld | Names the program. Has to match the file name. |
public static void main | Where Java starts running. Classes have not been covered yet, so ignore the words for now and just copy the line. |
System.out.println(...) | Prints whatever is in the brackets, then moves to the next line. |
; | Ends a statement. Like a full stop in a sentence. Java will not compile without them. |
Next steps
Make it say something else
HelloWorld.javaChange the message, save, compile, run. Then add a second println so it prints two lines.
Hello, world!
This is my first program.
Hint
A second System.out.println(...) line goes right under the first one, inside the same braces.
Break it on purpose
HelloWorld.javaDelete one semicolon and compile. Read the error. Put it back. Now delete a closing brace } and compile. Read that error too.
Knowing what a broken program looks like is the point. Both errors will appear many times.