Skip to content

Latest commit

 

History

History
234 lines (164 loc) · 12.7 KB

cse11-java-setup.md

File metadata and controls

234 lines (164 loc) · 12.7 KB

Note: Any recent version of Java will work for this course. The instructions below show Java 11 as an example, but Java 17, 21, or other versions are perfectly acceptable.

Installing Java (Example: Java 11)

Navigate to the following link: https://www.oracle.com/java/technologies/downloads/#java11

Depending on your operating system, select one of the following:
image

Installing Java on Windows

  1. Choose the x64 Installer image

  2. It will prompt you to create an Oracle account. Create one, and log in. If you receive a password validation error even though your passwords match, try creating the account in an Incognito/Private/InPrivate window. image

  3. Download the executable (you can place the executable called jdk-11.0.30_windows-x64_bin.exe anywhere you want; I usually leave it in the default Downloads folder).

  4. Run the executable that you just downloaded (by double-clicking it) and follow through the setup wizard
    image

  5. Install it in the default directory.

  6. You should now have this. You can click close:
    image

That's it!

Installing Java on MacOS

  1. If you have a Mac with Apple Silicon, choose the Arm 64 DMG Installer
    image

  2. If you have a Mac with an Intel chip, choose x64 DMG Installer image

  3. It will prompt you to create an Oracle account. Create one and log in. If you receive a password validation error even though your passwords match, try creating the account in an Incognito/Private/InPrivate window.
    image

  4. Download the executable.

  5. Open the .pkg installer and follow through the installation instructions
    image

  6. Click "Continue" and you should see this:
    image

  7. Provide your login credentials and click "Install". After installation, you should see the following:
    image

  8. Click "close" and you're done!

Note: It's perfectly fine to see version 11.0.xx (where xx is higher than 18) in the above screenshots.

Verify Java Installation (in two steps!)

ONE: Open your terminal.

  • For MacOS, press Command + Spacebar, type "Terminal", and hit Enter.
  • For Windows, press the Windows key, type "Terminal", and hit Enter.

TWO: Run the following command: java -version

You should now see the Java version you're using! (Below is an example of how this looks. I personally have Java 17)
image

If you run into issues when running these commands after you've installed Java, you may either need to:

  1. Restart your terminal and run the commands again
  2. Make changes to your system Path variable (though this is typically not necessary): https://www.java.com/en/download/help/path.html

If you have multiple Java versions installed and want to use Java 11:

If you are on Windows, ask on Piazza or come to office hours. We will provide information on this if there's demand.

If you are on a Mac, the following steps will show you how to set the default version of Java to your preferred version (Java 11 is used as an example below).

  1. Run the following command:

    $ /usr/libexec/java_home -V

    You should see output that looks like this:

    Matching Java Virtual Machines (3):
    
    12.0.2, x86_64: "Java SE 12.0.2"
    /Library/Java/JavaVirtualMachines/jdk-12.0.2.jdk/Contents/Home
    
    11.0.2, x86_64: "Java SE 11.0.2"
    /Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home
    
    1.8.0_131, x86_64: "Java SE 8"
    /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home
    
    /Library/Java/JavaVirtualMachines/jdk-12.0.2.jdk/Contents/Home
    

    Please ensure that you have a Java installation (any recent version will work).

  2. Pick your preferred Java version and set it to default by running the following command (Java 11 example):

    $ export JAVA_HOME=`/usr/libexec/java_home -v 11`

    You can also copy the exact version like this:

    $ export JAVA_HOME=`/usr/libexec/java_home -v 11.0.2`

Installing Visual Studio Code

We will be installing Visual Studio Code (VS Code) as our IDE to modify our Java programs.

Note: You can use any text editor (e.g., Vim, Notepad (Windows), TextEdit (macOS)) you want to edit the files in your programming assignments. You also have cloud-based options like GitHub Codespaces, which provides a complete development environment in your browser without requiring local installation. Do not use an Integrated Development Environment (IDE) that automatically generates source code, as 1) you need to learn how to write all source code yourself, and 2) the resulting source code often will fail to compile and/or run in Gradescope, resulting in a score of 0!

  1. Navigate to this link: https://code.visualstudio.com/download

  2. Download the respective executable:
    a. For Windows:
    image

    b. For macOS:
    image

  3. Follow the installation instructions depending on your OS.


Writing a small Java program

You will create a new Java file named Hello.java in Visual Studio Code.

  1. Open up Visual Studio Code

    a. If you are using a personal computer, you can open Visual Studio Code like you would with a normal application.
    b. Alternatively, if you are using the terminal (command line), you can type in code and it should open up Visual Studio Code as well (pretty neat, right?).

You should be prompted with a welcome page that looks similar to the one below:
image

  1. Create your working folder!
    a. Create a dedicated folder for CSE 11 and place it anywhere on your computer (e.g., in the Desktop).
image

b. Then, inside the CSE 11 folder, create a dedicated folder for your CSE 11 Programming Assignments (e.g., CSE 11 PAs) to stay organized.
image

c. Then, inside CSE 11 PAs, create one last folder (I promise!) for this small program to live on (e.g., PA0).
image

  1. Now, let's open our PA0 folder inside VS Code so we can start programming!
    Click on the Explorer tab and select "Open Folder" in VS Code.
    image

  2. Then navigate to the location where you stored PA0 (e.g., Desktop → CSE 11 → CSE 11 PAs → PA0), click on PA0 and then Select Folder.

image

  1. You should see the VS Code explorer showing the PA0 folder (currently empty).
    Click on the "New File" icon to create the file for this small program.
image

  1. Name this file Hello.java and hit Enter. Note that the file name starts with an Uppercase (this is important).
image

  1. Copy-paste the code below (this is called boilerplate code, it's code we use in all our programs to make them run!):
public class Hello {
    public static void main(String[] args) {


        
    }
}

You should see this on your screen:
image

  1. Notice that a white circle is next to the file name tab. This means your progress is not saved. Hit CTRL + S on Windows or CMD + S on Mac to save your file (and the white circle should go away)
image

Now we dive into the actual coding! Let's have this program print "Hello" first.

  1. Copy-paste this line inside the main function:
System.out.println("Hello");

You should have the following on your screen:
image

  1. Open the terminal by going to the Terminal menu in the top toolbar, then select New Terminal
image

This will open a terminal for your current folder (i.e., for PA0). You should see the following on your screen:
image

  1. To run our program using the terminal, we need to do a two step process:
  • Compile Hello.java by typing javac Hello.java in the terminal
image

and hitting Enter. (This will create some new files with the .class extension, don't worry about them).

image
  • Execute Hello.java by typing java Hello in the terminal (after you compiled in the previous step)
image

and hitting Enter.

image

You should see "hello" now! (As shown above).

Now let's use a single variable to have the program say "Hello World!"

  1. Copy-paste this line inside the main function AND before the print line:
String x = "World!";

You should have the following now:
image

  1. Then, modify the print line so it uses x to print "Hello World!" as follows:

Note that we added a space after "Hello" (i.e., "Hello "):

image
  1. Run the program again using the terminal and our two-step process from above:
image

That's it! You have successfully used a String variable as input to output your message with the desired word "World!".