Intro to Java week 0
HELLO_WORLD.JAVA

Running the first program

Open a file in jGRASP, compile it, run it. Ten minutes, and then every other week works.

Download week 0 One zip. Unzip it, and the file is ready to open.

Using jGRASP

  1. Unzip the download

    Double-click week-0.zip. A folder appears with HelloWorld.java in it. Keep all Java files in folders like this one, not loose on the desktop.

  2. Open the file

    Open jGRASP, then File → Open and pick HelloWorld.java. The code shows up in the editor in the middle.

  3. Compile it

    Build → Compile. This turns the code into something the computer can run. If nothing happens, that is good news. It worked.

  4. Run it

    Build → Run. The bottom pane switches to Run I/O and prints Hello, world!

  5. 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.

Save before compiling

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 rule that catches everyone

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!");
    }
}
LineWhat it does
public class HelloWorldNames the program. Has to match the file name.
public static void mainWhere 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.java

Change the message, save, compile, run. Then add a second println so it prints two lines.

Example output
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.java

Delete 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.