Installing and Using Java Compiler on Windows XP and Windows 7

January 19, 2014

0. Some Security Measures Before Installing Your Java Compiler

The U.S. Department of Homeland Security is urging computer users to disable Java software on their computers. An exploit for an unpatched vulnerability in Java is being used by cybercriminals to infect computers with malware. I advise you to disable java applications from running from your web browsers as follows. Running a Java compiler offline as we will for the course is okay though.

  1. Go Start and Control Panel. Look for the Java icon. (If your computer does not show the Java icon, do this: select View by: Small Icons for Windows 7; select “switch to classic view” for XP.) If you still don't see Java at this point, it is likely that Java has not been installed on your machine. Well, you will be installing it below, so come back and do this again after you install it.
  2. Click on the Java icon (it may take 5 seconds or so to open)
  3. Click on the Security tab at the top.
  4. If you are running the latest version of Java, there will be a check box at the top for “Enable Java Content in the browser”. Uncheck that box.
  5. Then click OK and OK and you are done. Do not bother clicking on the View button even if the enabled button is checked, it is disabled Java wide.
  6. If you are not running the latest version, there will not be a checkbox (only a button for “certificates” which is not what we want). So, click on the Java tab instead of the Security tab. Click on “View”. In the box that opens, on the far right column, uncheck the box for “Enable” . If there are multiple lines (for both Java 5 and 6 or 6 and 7) uncheck the box for “Enable” on all lines. Click OK and OK to exit.
  7. Important: Do this once more after you install Java below. When you checked this the first time, Java was probably not installed and you did not have to do anything. Well, now you will have Java installed and will have to disable it from your web browser.

1. Installing Java Compiler on a Windows 7 (or later) Machine

If you are not familiar with some of the DOS commands, try the DOS Lab before you try this.

Note: If you are using a Windows XP (as opposed to a Windows 7 or later) machine, see me (Art Lee).

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 7u51'. At the time of this writing, the latest version is '7u51'. If you see a newer version, use the latest one. (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 Download for Java Platform (JDK) 7u51.
  3. First, Accept License Agreement. Then, for Windows you will see two choices: Windows x86 and Windows x64. Download the .exe file for 'Windows x86' or 'Windows x64' depending on whether your machine is a 32-bit machine or 64-bit machine respectively. If you don't know whether you have a 32-bit machine or 64-bit machine, try Start -> Control Panel -> System (note that '->' means 'followed by') and check the System type.
  4. Download 'jdk-7u51-windows-i586.exe' or 'jdk-7u51-windows-x64.exe' into a folder somewhere on your computer. By default, it will probably download it on to your desktop or Downloads folder. (I personally like to keep all the downloaded stuff in one area. So, I have created a folder named downloads in my own area on the C: drive. So, I actually saved it in the C:\downloads folder on mine. This way, I will have a record of all the software I have downloaded and installed on my computer. It is my habit! If it has already saved to the desktop or some other folder, you can move it to one such folder using the cut and paste operations in a Windows Explorer window. When I say Windows Explorer, I don't mean Internet Explorer. Windows Explorer is the tool that shows file folders and files on your file system in a hierarchical fashion.)

    At this point go back to the previous page where you clicked on Download for 'Java Platform (JDK) 7u51'. Click on Installation Instructions under JDK 7 Docs. We will develop Java programs. Click on JDK Installation for Microsoft Windows under Microsoft Windows.

  5. Go to the Running the JDK Installer section and follow the instructions there. Or follow my instructions here instead if you like. (Some parts of the instructions on the SUN/Oracle website may not apply to your situation. So, I wrote the following which may be enough for you - it was enough on my Windows 7 machine.)
    1. Check the download size (123.64 MB or 125.46 MB depending on which of the two you downloaded) (Use the "Details" option under the "View" menu in a Windows Explorer window or check the "properties" of the downloaded file again using Windows Explorer.). Depending on the machine you use, it may actually show a slightly different number, which is ok.
    2. Optionally uninstall Java if one has already been installed. Even if you have not installed one yourself personally, there may be a version of JRE (Java Runtime Environment) installed. Uninstall it using the 'Add or Remove Programs' by following 'Start -> My Computer -> Control Panel'. When you install a JDK, JRE is included in it.
    3. Run the JDK installer that you downloaded earlier by 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.
      • Accept 'Development Tools' which will be your default selection. This will include JRE as well.
      • For the destination folder ("Install to:"), just accept the one they suggest. It will install JRE first followed by JavaFX 2.0 SDK.
      • It will ask you to register Java. Go ahead and do it if you wish (I did).
    4. You have just installed a version of both JDK and JRE now. Try Add or Remove Programs by following Start -> My Computer -> Control Panel again to see if 'Java 7 Update 51' is in the list of programs installed there. Also check the folder: 'C:\Program Files\Java' using Windows Explorer. There you will see two subfolders (subdirectories) named 'jdk1.7.0_51' and 'jre7'. This is where the installations reside in your file system.
    5. Update the PATH variable even if it says it is optional. This is how you do it on a Windows 7 machine:
      1. Follow Start -> Control Panel -> System and Security -> System (double click on 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. (I added it to the 'User variables for alee' 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\jdk1.7.0_51\bin;. You do it by using the New button. OK to complete.

        If PATH already exists, select it and use the Edit button to append the Java path value to the end of whatever is already there. Multiple values are separated by a semicolon (;). The value that you want to add is most likely C:\Program Files\Java\jdk1.7.0_51\bin; if you are installing "JDK 7 Update 51". This is the location where the Java compiler 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 effective in that window.
    6. Start using the JDK. See the next section.

2. Verifying Your Java Installation

Compile and run a simple Java program to verify your installation.

  1. To verify your Java installation, download Simple.java into a folder (aka directory), any folder.
  2. Open a DOS Command window (enter 'cmd' into the input pane after clicking on Start) and use the 'cd' command to go to the directory where Simple.java is located.
  3. Issue the following command to the DOS prompt to compile the source file (including the quotation marks):
       "C:\Program Files\Java\jdk1.7.0_51\bin\javac" Simple.java
       
    This will create a new file named Simple.class in the same directory where Simple.Java is located. You can check that using the dir command, right? That is a lot of typing if you are compiling a lot. If you set the PATH variable correctly, you should be able to issue the following command to achieve the same effect:
       javac Simple.java
       
  4. The value associated with the PATH variable tells the operating system (the DOS operating system in our 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).

  5. Issue the following command to run the program (again including the quotation marks):
       "C:\Program Files\Java\jdk1.7.0_51\bin\java" Simple
       
    Or, the following if you set the PATH variable correctly:
       java Simple
       
    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 slight 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 definitions of the commands ('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 by following Start -> Control Panel -> System -> Advanced 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 '.;' (that is a dot followed by a semicolon without the single quotes) if it is already defined. If '.;' is not included, add it to the beginning of that value by using the Edit button (We are not replacing it but adding it). OK to complete. Close your DOS window and open a new one and try 'java Simple' again. If this does not fix your problem, you made a mistake somewhere else.

3. Installing Java SE 7 Documentation on your Windows machine

If you work on your computer offline a lot, you may want to have a copy of the Java documentation installed locally on your computer. If so, follow these instructions to install a copy.

  1. Go to the Java SE Downloads page.
  2. Scroll down to Java SE 7 Documentation and download and install it.
  3. Unzip the downloaded file into a folder. In my case, I unzipped it under "C:\Program Files\Java\". It will create a folder named "docs" under that directory. If you view "C:\Program Files\Java\docs\api\index.html" by dragging it to your web browser, you will see the home of the documentation site.

4. Some Security Measures After Installing Your Java Compiler

Now that a JRE is installed, go back to Section 0 and disable Java for your web browser.

That's it!