command to run a Java program), various other programming tools such as
javadoc, plus docu- ... http://java.sun.com/products/jdk/1.2/download-windows.
html.
Computer Science, University of Richmond
2005
Java Programming for Windows XP Users All of the software we use in CMSC 150 and CMSC 221 is freely available for the Windows platform.
Obtaining the Free Software You can get the Java 2 Standard Edition (J2SE) which contains javac (the compiler), java (the command to run a Java program), various other programming tools such as javadoc, plus documentation and the netBeans IDE at no charge. The current version of the J2SE is 1.5, and the current version of netBeans is 4.1. J2SE 1.5 bundled with netBeans 4.1 is a 128MB download; J2SE by itself is 56MB. The Java documentation is another 44MB. If you have the space, go to Sun’s web site and download the JDK 1.2. http://java.sun.com/products/jdk/1.2/download-windows.html
You will have to agree to a user license, which you should read before agreeing. Save the download in a temporary directory like C:\temp. Also download the documentation if you want. This will allow you to look up information on classes in the library without going across the Internet to get to Sun’s online copy. Downloading the documentation is not essential if you have limited disk space.
Installation Issues The installation instructions are found here: http://java.sun.com/j2se/1.5.0/install-windows.html
Installation is more or less automated. You can follow the basic installation procedure and install the files in the default location or you can create a directory like C:\java and install in C:\java\j2se1.5.0. I personally prefer having a special Java directory, primarily because in the past there were certain IDEs and third party libraries that had problems with pathnames containing spaces, but most of those problems should be history by now. However, if you do end up installing third party libraries, it is nice to have everything in one place. For the rest of this discussion I will assume that you installed in C:\Program Files\java\jdk1.5.0. I will refer to this as the “installation directory.” After installation you can delete the file you downloaded. After installing the programs you will need to set some environmental variables so the operating system can find the javac and java commands and the programs can find any additional java libraries you might install. The two key environmental variables to set are Path and CLASSPATH. After the JDK is installed, right-click “My Computer” and select “Properties.” In the window that appears, select the “Advanced” tab. Near the bottom of the resulting window, click the “Environment variables” tab. You can also check this from a Command Prompt window by typing the command set
and look to see if you can find Path and CLASSPATH among the list of values. There will be two windows, one with the environment variable values for the system as a whole and one for additions for the current user. If you are on a single user machine, it doesn’t make a lot of difference which one of these you modify. For our current purposes, look through the values in both
Page 1
Computer Science, University of Richmond
2005
windows. We want Path to include your installation directory’s subdirectory called “bin”. For example, Path=C:\windows\system32;C:\windows;C:\Program Files\java\JDK1.5.0\bin;
would be a reasonable value. This may be automatically set up for you during the installation process. Notice the semicolons. These separate directories in the list. The Path simply is a list of directories where the operating system looks for commands to run. If you see a similar path containing “jre” rather than “jdk,” you need do nothing. JRE stands for “Java Runtime Environment,” and contains its own bin and lib subdirectories. The CLASSPATH variable tells the Java compiler and interpreter where to find the classes it needs. It does not need to contain the location of the basic java libraries, only additional libraries you have installed yourself. For example, in the classpath ClassPath=.;C:\java\classes\junit.zip
the dot (.) means always check the current directory and the second listed entry indicates a collection of files in the archive junit.zip that are stored in the folder C:\java\classes which the user created. If you haven’t installed any additional class files or libraries, you need not modify this variable, either.
Where To Put Your Files I suggest creating a folder like C:\java for your working space. In that folder create a folder called classes for any additional class libraries you may install. If you have special projects you can create folders in C:\java to do your work.
Creating Java Programs on a PC with the JDK You will need to use an editor or IDE. You can use Notepad for the editor although Wordpad will work just as well. There are also a number of free Java editors as well as more capable software development environments like NetBeans and Eclipse. If you are interested, you can have a look at http://www.mathcs.richmond.edu/~lbarnett/java/Java.php for a number of links. The following description assumes you are using the simplest possible option. •
Pick a directory/folder where you want to develop your programs. I suggest something like C:\java. • Start Notepad by selecting ‘Programs’ on the ‘Start’ menu, then ‘Accessories’, then ‘notepad.’ (Start>Programs>accessories>notepad) • Type in your program. You will have to indent manually. • When finished typing, select ‘Save As’ on the ‘File’ menu. Be sure you find the directory C:\java and then enter the name, including the .java extension. • To compile the program requires an MS-DOS window. Open one by selecting ‘Programs’ on the ‘Start’ menu, then ‘Command Prompt.’ • In the command prompt window (which is a simple command-line terminal window) enter the following commands C: If the prompt is already something like C:\WINDOWS> you would need to enter cd .. to get the prompt C:\> Then continue with the commands below.
Page 2
Computer Science, University of Richmond
2005
cd java dir The last command will list the files in the current directory. Look for your file.
•
If your file was saved as Values.java, then compile it with javac Values.java • If you have errors, go back to Notepad and fix them. Then re-save and give the compile command again. • If you are ready to run the program, type java Values The file can be printed to your local printer (from Notepad’s File menu). It can be mailed to me as an attachment using Eudoria or Netscape mail. You can also use ftp to transfer the file to your Unix account.
Page 3