Installing and Using Java Compiler on Windows 10

July 28, 2020

1. Installing Java Compiler on a Windows 10 Machine

Warning: Once you make a mistake during your installation, it is really hard to figure out where you made the mistake. So, follow the instructions carefully.

Follow these steps:
  1. Go to the Java SE Downloads page. We are installing 'Java SE 14.0.2', which is the latest version at the time of this writing. If you see a newer version, use the latest one, in which case you make appropriate changes in the instructions below by using the correct version number. (You may want to open another copy of this page on a different tab if you don't want to go back and forth.)
  2. Click on the 'JDK Download' button on the right side of the screen to reach the Java SE Development Kit 14 Downloads page.
  3. First, Accept License Agreement. Then, for Windows download 'jdk-14.0.2-windows-x64_bin.exe' into a folder somewhere on your computer. By default, it will probably download it to the Downloads folder.
  4. Now, run the JDK installer that you downloaded above ('jdk-14.0.2-windows-x64_bin.exe') by double-clicking on it from a Windows Explorer window. You will be clicking on the Next button most of the time. If you are not sure, just take the default option that is being offered to you.
  5. You have just installed a version of both JDK and JRE now. Look in Control Panel to see if 'Java' is in the list of programs installed there. Also check the folder: 'C:\Program Files\Java' using Windows Explorer. There you will see a subfolder (subdirectory) named 'jdk-14.0.2'. This is where the installations reside in your file system.
  6. Update the PATH variable. Note that you must be a user with administrator access.
    1. Go to Control Panel followed by System and Security followed by System.
    2. Click on Advanced system settings in the left pane.
    3. Select the Advanced tab.
    4. Select the Environment Variables button.
    5. You will see two sections: 'User variables for YourName' and 'System variables'. If you add it to the 'User variables' section, the PATH variable will be in effect only when you are logged on. If you add it to the 'System variables' section, it will be in effect as long as someone is logged on. If you are not sharing the machine with anyone else who writes Java programs, you may add it to either section.

      Capitalization of the name does NOT matter. That is, PATH is considered the same as path or Path.

      If a PATH variable does not already exist (most likely it is already there), then add a new variable named PATH, with the value C:\Program Files\Java\jdk-14.0.2\bin. You do it by using the New button. OK to complete.

      If PATH already exists, select it and use the Edit button to add the Java path value as the last item (separated by a semicolon, ';') or as the last line. The value that you want to add is most likely C:\Program Files\Java\jdk-14.0.2\bin if you are installing "JDK 14.0.2". This is the location where the Java compiler ('java.exe') was installed in your file system. OK to complete.

    6. If you already have a DOS Command window open, close it and open a new one to make the PATH variable take effect in that window.
  7. You should now be able to use the JDK. You can verify this using IntelliJ IDEA or using the command line as described in the next section. If you want to use IntelliJ IDEA for now and try command line later, your choice!

2. Verifying Your Java Installation

Compile and run a simple Java program to verify your installation on the command line. If you are not familiar with some of the DOS commands, try the DOS Lab before you try this.

  1. To verify your Java installation, with a right click and using Save Link As download Hello.java into a folder (aka directory), any folder.
  2. Open a DOS Command window (enter 'cmd' into the search pane) and use the 'cd' command to go to the directory where Hello.java is located.
  3. Issue the following command to the DOS prompt to compile the source file:
       javac Hello.java
       
    This will create a new file named Hello.class in the same folder where Hello.Java is located. You can check that using the dir command.

    The value associated with the PATH variable tells the operating system (the DOS operating system in this case) to look in those places (all the folders included as the Value of the PATH variable) for the definition of the command that you are issuing (javac in this case).

  4. Issue the following command to run the program:
       java Hello
       
    This will produce the output that you expected if your installation was done correctly.

    Even if the PATH variable is set correctly, there is a small chance that the 'java' command may not work even if the 'javac' command worked. If that happens, it is most likely that the CLASSPATH variable is already being used by some other program already installed on your computer. The value associated with the CLASSPATH variable tells the operating system (e.g., DOS) to look in those places (all the folders included as the Value part of the CLASSPATH variable) for the .class file that you are trying to run with the 'java' command. That is, PATH tells DOS where to find the definition of the command ('javac' and 'java'), whereas CLASSPATH tells DOS where to find a .class file to run.

    If your 'java' command does not work, check to see if CLASSPATH (uppercase or lowercase letters or some mix of uppercase and lowercase letters) is defined in the Environment Variables section as before. If defined, check the value of CLASSPATH. (One student reported to me that the value his had was C:\Program Files\QuickTime\QTSystem\QTJava.zip.) The value of CLASSPATH should start with a line containing a '.' (that is a dot without the single quotes) if it is already defined. Close your DOS window and open a new one and try 'java Hello' again. If this does not fix your problem, you made a mistake somewhere else.