Skip to content

Latest commit

 

History

History
139 lines (81 loc) · 6.69 KB

jdk17.md

File metadata and controls

139 lines (81 loc) · 6.69 KB

We’ll be using OpenJDK 17 as our grading environment. For maximum compatibility, please make sure you are using JDK 17 locally. However, you are free to use any IDEs you want (but you should not use any AI-assisted code completion plugins).

Initial Setup

For macOS

(For Windows, skip to the section below)

  1. Check if you have the up-to-date Java installed.

    Type the following command in your Terminal:

    java -version

    The output of the above command should give: java version “X.X.X.X”, where X.X.X.X specifies the version number. If your Java version is not 17.x.x.x, then you need to get the updated version. If an error gets produced saying there is “No Java runtime present”, then you have to perform a fresh installation.

  2. Download Java.

    You may follow this guide if you can’t tell if your Mac is Intel-based or M1/M2-based.

  3. When you’ve finished the download, click it and follow the installer. You might also have to double click the box (shown in the first photo below) to start the installer (you can keep the default options).

  1. Re-run the following command in your terminal to ensure you have the correct version.

    java -version

    It should now give you a version number that is 17.x.x.x.

For Windows

  1. Check if you have the up-to-date Java installed.

    Type the following command in your Command Prompt:

    java -version

    The output of the above command should give: java version X.X.X, where X.X.X specifies the version number. If your Java version is not 17.x.x.x, then you need to get the updated version. If an error gets produced saying there is “No Java runtime present”, then you have to perform a fresh installation.

  2. Download Java: https://download.oracle.com/java/17/latest/jdk-17_windows-x64_bin.exe

  3. When you’ve finished the download, click it and follow the installer.

  4. Type javac in the command prompt and see the output, if you get javac is not recognized as an internal or external command, this means the JDK Path is not set. To set it:

    1. Find the location of your Java folder (by default Java is installed in C:\Program Files\Java).  Go to the Java folder, and then go into the bin folder, which is inside the Java folder (by default bin can be found at C:\Program Files\Java\jdk-17.0.4.1). In the bin folder, you should see the files javac.exe and java.exe.  Highlight the path and copy it as shown below.

    explorer_4AhtNeuZsq

    1. In explorer, right-click on “This PC”, and select “Properties”.

    2. Scroll down to find “Advanced System Settings”.

    3. Open “Environment Variables”, select the variable named ‘Path’ and press ‘Edit’ under the ‘User Variables for xxx’ section to edit the Path environment variable.

    4. Click New and paste the path (e.g., C:\Program Files\Java\jdk-11.0.5\bin) to your Java bin folder. Click OK.

      ezgif-1-bffa6d014d

    5. Finally, open a new command prompt window again and type javac. You should see something like below.

      
      \> javac  
      Usage: javac <options> <source files>
      where possible options include:
      ...
      

Writing your program in an IDE

  1. Download and install VS Code (https://code.visualstudio.com). You can keep all the default options.

  2. Create a new folder on your preferred location (e.g Desktop, Documents) to save our code files.

  3. Open the folder you just created by clicking “File” - “Open Folder”, and then proceed to select the folder you just created.

  4. Create a new file by clicking “File” - “New Text File”, and then type the following HelloWorld program in the text editor.

    
    /**
     * Name: Qingyang Hu
     * Email: [email protected]
     * PID: A16164360
     * This is an example Java file to demonstrate how to print
     * a message to standard output.
     */
    
    /**
     * This is the class for our HelloWorld program. You may modify the message by
     * changing the MESSAGE variable.
     */
    public class HelloWorld {
        private final static String MESSAGE = "Hello World!";
        /**
         * This is the main method of the program, and it'll print a Hello World
         * message to standard output.
         * @param args not used in this program
         */
        public static void main(String[] args) {
            System.out.println(MESSAGE);
        }
    }
    
  5. Once you’re done typing, choose “File” then “Save” then change type to “Java” and save the file as HelloWorld.java.

    ezgif-1-40e1001e1c

Compiling and executing your program

  1. In VS Code, click on the Explorer icon on the top left to see the files. Then right click on “HelloWorld.java”, and click on “Open in Integrated Terminal”.

    image

  2. Type the following in your Terminal to compile your code:

    javac HelloWorld.java

    You should not see any error messages. This will produce a file named HelloWorld.class, which will store information about the file you just compiled.

  3. Finally, to run the file, type the following (make sure you do not add the “.class” part at the end of the file name):

    java HelloWorld

    You should see “Hello World!” printed in your Terminal.