handout #2
CSE142—Computer Programming I
Installing Java
For this course we will be using a combination of two software products: the Java tools from Sun Microsystems and a simple editor called TextPad. The Java tools are known as the JDK (Java Development Kit), which is distributed freely by Sun Microsystems. It includes a Java compiler and a Java runtime system. TextPad is a shareware text editor. The Computer Science and Engineering Department has purchased a site license for the use of TextPad to support CS instruction. Both of these have already been installed on the machines in the Introductory Programming Lab on the third floor of Mary Gates Hall.
Getting the
JDK and TextPad on a Windows Machine
We are using the latest version of the Java compiler known as version 1.5 or “Java 5.0”. You can obtain a copy at this url:
http://java.sun.com/j2se/1.5.0/download.jsp
There are several versions available on this page. Be sure to follow the link that is labeled “Download JDK 5.0.” Install the JDK before you install TextPad. You can get TextPad from:
http://www.textpad.com/download/index.html
TextPad should automatically configure itself for use with the JDK, but there are a few settings you might want to choose. Go to the “Configure” menu and select “Preferences.”
1. If you want to see line numbers in the editor (because the Java compiler gives line numbers when it reports errors), select “View” and “Line numbers.”
2. If you want Java files to automatically open in TextPad, select “Associated Files.” Under “New extension” select “.java”. Then click on “Add”.
3. If you want TextPad to use Java files by default when bringing up a file dialog, select “File Name Filters.” You will see a list of filters including “.java”. Select it. Then click on “Move up” until it is at the top of the list.
4. You can avoid problems with any classpath setting by setting your own. Select “Tools” and “Compile Java” and change this setting:
Parameters: |
$File |
By adding a classpath of “dot” (the dot is required):
Parameters: |
-classpath .
$File |
And under “Tools” and “Run Java Application”, change this setting:
Parameters: |
$BaseName |
By adding a classpath of “dot” (again, the dot is required):
Parameters: |
-classpath .
$BaseName |
For any of these options to take effect, you will have to select “apply”.
Exception in thread "main" java.lang.NoClassDefFoundError
Make sure that you have successfully compiled the program inside of TextPad (it should beep when you give the command “Compile Java”). If the program compiles properly and you still get this error, then you probably have a setting for an environment variable called “classpath”. You can verify this by going to Programs/Accessories/Command Prompt and giving the command “set c”. This will show you all of your variables that begin with the letter “c”. If you see a setting for classpath, then that’s your problem. To fix it go to the "my computer" icon, right click and select properties, select the "advanced" tab, click on the "environment variables" button and find and delete the setting for classpath.
Any OS X operating system will have Java available automatically. Unfortunately, TextPad does not run on the Macintosh. There are several other programming environments that you can choose from. Most beginners prefer DrJava, which can be downloaded at the following site (download the “development release” because it has important new features that are not part of the stable release):
TextPad has commands to compile and run a program. The equivalents in DrJava are the “Compile All” tab and under the Tools menu, “Run Document’s main method.”
To be able to use the latest Java (Java 5.0), you have to have the Tiger operating system (OS X 10.4 or higher). If you have an older operating system like Panther, you might want to consider buying the upgrade ($69 at the university bookstore, which is a discounted price). Even with Tiger, you won’t get the new version of Java automatically. You have to go to this web page to download it:
http://www.apple.com/support/downloads/java2se50release1.html
This still isn’t enough because you
have to run a program to update the default version of Java. The utility is called “Java Preferences” and can
be found in the folder /Applications/Utilities/Java/J2SE 5.0/. When you run that utility, the key section is
in the middle of the dialog box and is calle “Java
Application Runtime Settings.” Click on
the “J2SE 5.0” text and drag it to the top of the list to make sure it is the
one used by DrJava and other programs.