APCS – Unit 2 – Lab 2.2 – Part 1: HelloWorld using JDK and Command Prompt

It is a tradition to write a Hello World program when a programmer learns a new language.  The program is often used to show how the process of coding works and to ensure that a newly installed programming language or system is operating correctly.

Today in our lab activity, we will write the HelloWord Java program using a text editor, and compile and run the program using JDK and command prompt.

Review:  Java Edit – Compile – Interpret Process

Before we start, let’s recall this diagram that illustrates the process of Java software development and distribution through the Internet. Can you fill in the blanks?

The process is as follows:

First, Saving a Java Program 

You can use any editor or word processing program to create and edit a Java program, as long as it can save files in ASCII format. Java programs must be saved in a file whose name ends with the .java extension. It’s called the source file.

Second, Compiling a Java Program

Once the Java program is created, you must compile your program using the Java compiler before can you run it.

The Java compiler translates Java source code into Java bytecodes which are components of a machine-level language for the Java virtual machine. Java bytecodes are interpreted and executed by the Java interpreter.

When you compile a Java source file, the compiler creates a file with the .class extension in the same folder as the source file. The compiler names the resulting .class file after the class defined in the source file. 

Last, Running a Java Application

Now, you can run your application using the Java interpreter. 

IMPORTANT RULES TO REMEMBER:
  • Java is case sensitive!
  • The name of Java file is also name sensitive!
  • The name of the file that holds a Java class must be exactly the same as the name of that class (plus the extension .java)!

Lab Instruction: Create and Run a “Hello, World” Program Using JDK and Command Prompt

JDK itself doesn’t have an IDE, and JDK tools are UNIX – style like command-line tools. The compiler is called javac.exe, the interpreter is called java.exe. These programs are stored in the bin folder where your JDK is installed.

 

If you are using a Windows PC  or laptop:

STEP 1: Create a new directory on your C: drive and name it “MyWork“. You can do this through File Explore –> Create New Folder.

STEP 2: Open NotePad, and enter the following program.

public class HelloWorld {
    public static void main(String[] args) {
         System.out.println(“Hello, World!”);
   }
}

If you use a word processor like WordPad or MS Word, make sure the file is “TEXT ONLY”. 

STEP 3: Save the file as HelloWorld.java in “MyWork” directory.

When you save the file, choose “All Files” as the Save as type, and the file name extension must be .java. Choose ANSI as Encoding.

STEP 4: Start a CMD Shell (Search ⇒ enter “cmd” ⇒ select “Command Prompt”).

STEP 5: Once the Command Prompt window is open, navigate to the folder that contains your program (MyWork).

From your current directory to the directory that contains your program use the cd command:

C: [your current directory] > cd  MyWork

STEP 6: Invoke the JDK compiler “javac” to compile the source code “Hello.java“.

C:MyWork>javac HelloWorld.java

If your prompt tells you “javac not found”, please verify that you have followed the instruction in the previous lab and set the path.

If error message appears, open the source file HelloWorld.java and correct the error.

STEP 7:  The output of the compilation is a Java class called “Hello.class“. Issue a dir (List Directory) command again to check for the output.

STEP 8: To run the program, invoke the Java Runtime “java“.

If you are using a Mac PC or laptop:

STEP 1: Write a HelloWorld Java Program
  1. Create a directory called “MyWork” under your home directory (Launch “Finder” ⇒ “Go” ⇒ “Home”; Select “File” ⇒ “New Folder” ⇒ “MyWork”).  In macOS, the home directory of the current user can be referenced as “~“. Hence, this new directory can be referenced as “~/MyWork“.
  2. Use a programming text editor (such as Sublime Text or Atom) to input the following source code and save as “HelloWorld.java” under the directory “~/MyWork“.
    (If you use macOS’s default text editor “TextEdit” (NOT recommended), you need to open a new file ⇒ choose “Format” ⇒ “Make Plain Text” ⇒ Enter the source code ⇒ Save as “HelloWorld.java“.)
    /*
     * My First Java program to say Hello
     */
    public class HelloWorld { 
       public static void main(String[] args) {
          System.out.println("Hello, world from Mac!");
       }
    }
STEP2: Compile and Run the Hello-World Java Program
  1. To compile the source code “HelloWorld.java“, open a new “Terminal” (“Go” ⇒ “Utilities” ⇒ “Terminal”) and issue these commands (as illustrated):
    // Change Directory (cd) to where "HelloWorld.java" resides
    cd ~/MyWork
    // Check if "HelloWorld.java" exists using list (ls) command
    ls
    HelloWorld.java   ......
    // Compile "HelloWorld.java" using JDK compiler "javac"
    javac HelloWorld.java
    // If error message appears, correct your source code and re-compile
    // Check for the compiled output "HelloWorld.class"
    ls
    HelloWorld.class   HelloWorld.java   ......
  2. To run the Helloworld, invoke the Java Runtime “java” as follows:
    java HelloWorld
    Hello, world from Mac!
 
List of Sources and References: