Last revised: 2002/09/16 by Russ Tront
Previous revisions by: Eddy Yip
Original taken and significantly adapted from tutorials by Tony Dixon, Russ Tront, and Mike Evans.
This tutorial teaches you the basics of using Java in the SFU Assignment Lab. It includes a step-by-step guide of accessing and running a Java program along with two example Java programs.
Table
of Contents:
Using Java in SFU Assignment Lab Tutorial
A. Getting Started In the Assignment Lab
C. Compile/Run Your Program Using Command Prompt
D. Compile/Run Your Program Using Textpad
E. Handling Java Error Messages
G. Doing Your Assignments at Home
For those who intend to run this at home, please change directory and path names mentioned here to accommodate your own machine. For more information on doing your work on your home computer, see the appendix section below titled “Doing Your Assignment at Home” and also any information from your instructor.
Before you enter anything into the computer, it is a good idea to develop a design and construct a draft of your source code on paper (unless you are going to just test one of the programs in your textbook). This will save you time at the workstation and may reduce the number of logical errors that occur as a consequence of failing to include something. You can even use the Notepad text editor at home to enter a program into a file at home, before coming to the Assignment Lab. Notepad is available in MS-Windows from the start menu: Start>Programs>Accessories>Notepad.
Sign on to the ACS Assignment Lab PC, and provide a formatted
3.5” floppy disk in order to store your work between visits to the Assignment
Lab.
In a new document, type in the following code:
/** * CMPT101 * Assignment 0 * Instructor: Russ Tront * * This class "FirstClass" will display the result 2 * pi on the screeen * you can find these codes in your section 3.1 notes) */ public class FirstClass { // define a constant static final float PI = 3.14159F; // here is the main function public static void main(String[] args) { float twoPi; // define a variable twoPi = 2.F * PI; System.out.println(twoPi); } } |
Be
Careful When You Type Type all code, commands, and file names exactly as shown. The Java compiler and interpreter are case-sensitive, so you must capitalize consistently.
|
(Note: Do not choose or make a path that contains blank characters; Java doesn’t like this)
"FirstClass.java"
. (Note: the file name is the same as the class name, but with a .java suffix.)
When you're finished, the dialog box should look like this:
Now click Save.
Almost all computer program, including the Java compiler and your resultant program can be run from the Operating System (OS) command prompt (also called command line interface, or command shell). This section shows you how to do that. The next section shows you how to more conveniently do this directly from Textpad.
The prompt shows your current directory. When you bring up the
prompt for Windows 95/98, your current directory is usually WINDOWS
on your C
drive. On other versions of your OS it may be some
other disk and directory (as shown above).
Use the DOS command "cd" to define the directory path to your project folder. For example if your program is in the folder "CMPT101" on drive "A" then type "cd Cmpt101" and press return. To confirm that you have indeed made your project directory the active directory you should use the "dir" command to list all the files in the active directory. Your source files should be among those listed. To compile your file, type the command “javac” followed by your entire file name. For example, at the prompt, type the following command and press Enter:
|
If the OS prompt reappears without error messages, congratulations; you have successfully compiled your program.
However, if it does not, it may be that the OS cannot find the javac command. The java compiler in Assignment Lab is stored in “C:\jdk1.3.1\bin”. Though this should already be part of your OS search PATH, if it is not, you need to specify the complete path for Java compiler.
|
Error Explanation
If you receive this error, Windows cannot find the Java compiler, Here's one way to tell Windows where to find
Note: If you
choose this option, then each time you compile or run a program, you'll have
to precede your |
Once you successfully compile your
file, you will have a corresponding .class
file as shown below.
In whatever directory your program is in, enter at the prompt:
|
Note that you should not add the .class suffix.
The value of 2 times the mathematical constant Pi=3.14159 will be displayed something like this:
Congratulations! Your program works.
You can also compile and run your program more conveniently using TextPad.
Since this is a temporary window, it will disappear as soon as you program is finished. However, Textpad is nice enough to add (through a very sneaky mechanism) a “Press any key to continue ...” prompt that will keep the window open so you can see your program results.
Error Explanation
If you receive this error, or one containing the name of your program
instead of HelloWorldApp, One of the places
The prompt should change to If you still have problems, you might have to change your CLASSPATH variable. To see if this is necessary, try "clobbering" the classpath with the following command:
Now enter |
In most cases, your program will unfortunately not successfully compile when you first time compile it. You will likely to see many error-messages. Don’t panic. It is common when learning a new programming language. Try to understand what those error messages mean and make correction accordingly. Here, we will make some changes to the program in order to see some common error messages.
public class FirstClass { // define a constant static final float PI = 3.14159F; // here is the main function public static void main(String[] args) { float twoPi; // remove the semicolon here twoPi = 2.F * PI; System.out.println(twoPi); } } |
An
error message “FirstClass.java:20: ‘;’ expected” is displayed. The statement :
Float twoPi;
is on the line 20 in your FirstClass.java document.
If
you double click on the line A:\Cmpt101\Assignment0\FirstClass.java:20 ‘;’
expected, TextPad will switch to the file frame containing your source code and
put the cursor on the line 20 where “float twoPi” is located. Sometimes when compiling a program the
compile will have to compile several related files. Clicking on the first line of an error message will take you to
exactly the right line in the right file.
It is very common to write a big program in several smaller files. Here is an example. This program will request the user to input two strings and state if the second string is a substring of the first string. This program makes use of another pre-written class called SavitchIn that is provided by the textbook [CD-ROM from Savitch2001]. It is helpful for you to handle inputting from keyboard, which is otherwise rather difficult in Java. The SavitchIn helper class is already stored in the Assignment Lab. You can find it by following the path “N:\CMPT\101\tront\SavitchIn.java” or the similarly named 104 directory. To get it, open the Windows Explorer by selecting Start > Programs > Window Explorer. Copy the “SavitchIn.java” file into the directory where you are going to put the substring source file. For example, create a folder “Assignment0.1” in the folder “A:\Cmpt101”. Copy the file “SavitchIn.java” from “N:\CMPT\101\tront\” to “A:\Cmpt101\Assignment0.1”. Compile the file SavitchIn.java using TextPad, or “javac” in Command Prompt.
Note: You should compile the SavitchIn.java before compiling your source codes (though the compiler might do this for you if you don’t).
Type in the following program that uses SavitchIn.java using TextPad, and save it as FindSubstring.java in the folder “A:\Cmpt101\Assignment0.1”. You can now compile your FindSubString.java file and run it.
/** * CMPT101 * Assignment 0.1 * Instructor: Russ Tront * Created by: Eddy Yip * * The class
"FindSubstring" will request user to input * 2 strings. We use the
SavitchIn class for our inputting * If the second string
is a sub-string of the first string, * then the program will
display the position where * the second string
first occurs in the first string. */ import java.io.*; public class FindSubstring {
public static void main(String[] args) {
System.out.println("Please input two strings.");
System.out.println("Input the first string: "); String
firstString = SavitchIn.readLine(); System.out.println("Input
the second string: "); String
secondString = SavitchIn.readLine(); int
pos = firstString.indexOf(secondString); if(pos
< 0) { System.out.println("The
string \"" + secondString + "\" is not
found in the string \"" + firstString +
"\"."); } else { System.out.println("The
string \"" + secondString + "\" is firstly
found in the position " + pos + " of the
string \"" + firstString +
"\".");
} } } |
Finally, try changing some codes in the above program to create errors, see what happen. Have fun.
Note
that if you are working at home and have a high speed Internet connection, then
you can download the Java Development Kit from http://java.sun.com/j2se/downloads.html
and Textpad from www.textpad.com. If you are having problems getting your
installation to work, you might want to look at Sun’s Java Tutorial at http://java.sun.com/docs/books/tutorial/ Note:
You are not allowed to surf the Internet from inside the Assignment Lab;
the lab is strictly for working on computer assignments. From Sun’s tutorial web page you can follow
the links as indicated below.
· Your First Cup of Java link to java.sun.com/docs/books/tutorial/getStarted/cupojava/index.html
· First Steps (Win32) link to http://java.sun.com/docs/books/tutorial/getStarted/cupojava/win32.html
· Installation Instructions link to http://java.sun.com/j2se/1.4/install-windows.html. In particular, if you cannot compile or run Java programs, pay attention to the section on Path.
If working at home, make sure you install the Textpad text editor after the Java Development Kit, so it can find and use the JDK for compiling and running Java programs.
If you have a slow modem, or if you are looking for further hints on starting to as a Cmpt student, or as an Assignment Lab user (e.g. where the Java online documentation can be found in the Assignment), see Russ Tront’s Cmpt 101 hints page at:
http://www.cs.sfu.ca/CC/101/tront/hints.html
Note that you should
be able to surf to this page from the Assignment Lab because it is not off
campus.