In order to run NOTEPAD, you click on the START menu button in the lower left corner and select RUN. Then enter
notepadand a window with the notepad editor will pop up. You then type your Java program. In order to save it, choose FILE and then SAVE where you enter the name of your Java program. (You only need to enter the name of the program the first time, afterwards notepad will remember the current file name.) This name should end in .java and at the SAVE should be entered in double quotes, like, e.g.,
"Hello.java"where the quotes are important so that notepad does not add the ending .txt, which it normally does, to the filename. It is important that you know where your program text is actually saved. This is typically your so-called H: space, namely a central file server that allows you to access your data, such as your Java program, independently from the computer that you are using. Make sure that this is where your program is saved. You can typically find it with the Windows explorer on your desktop under MY DOCUMENTS.
In order to get javac to work on your Java program, click on the START menu button in the lower left corner and select RUN. Then enter
cmdwhich starts a black window containing a so-called command interpreter, also known as a DOS prompt. This is an ancient way of talking to the computer known from pre-windows days as the Microsoft DOS operating system (which started all of Microsoft's riches as it ran on all IBM- and compatible PCs as the operating system for those computers, back in the 1980's). You are there greeted by
H:\>after which whatever you type will appear as text. If you do not get H:\> but, for example, C:\WINDOWS\Desktop>, you change to your H: space by typing H: followed by the ENTER key, where what you type looks in the first line like
C:\WINDOWS\Desktop>H: H:\>and the second line is the desired working directory on H: now.
You then type javac Hello.java, so the prompt looks like
H:\>javac Hello.javaand after hitting the ENTER key this sets the compilation of your Java program in motion. Obviously, Hello.java is the name of your Java program, which could be any other name if you named your program otherwise.
Typically, javac has noted some syntax error and then you have to correct your program in the notepad window, and save it and try to compile it again, until it is syntactically correct.
When you have succeeded in compiling the program, you will find in your directory (under H:) a new file called Hello.class. You can see this by looking at the folder with the windows explorer in MY DOCUMENTS, or by typing at the DOS prompt
H:\>dir *.java *.classwhich will list all files ending in .java and .class. (The star acts as a wildcard character matching any file name.)
Note: if your compilation was not successful but you did have a successful compilation before, Hello.class might be the old file and it will not behave as you expect. Check the file creation time (either in the Windows explorer or with the dir command) and make sure that your .class file is younger than the corresponding .java file. Alternatively, you may choose to delete all .class files before you start javac. In the DOS prompt, this is done by
H:\>del *.class
The class generated by javac Hello.java is actually only called Hello.class if the file Hello.java is of the form
class Hello
{
...
}
It is advisable to name the Java file and the class it contains
in the same way (here Hello).
If your file Hello.java contained instead
class Poop
{
...
}
then javac Hello.java would generate the class
named Poop.class.
In particular, this kind of stuff will happen if your Java
file specifies more than one class. (See below, not relevant
at the moment.)
H:\>java HelloNote that the command is java, not javac, and that the ending .class must not be given. Otherwise this won't work. It will also not work if your .class file is missing, of course. Finally, it is important to use lower and upper case correctly. The files hello.class and Hello.class are different!
The part that is executed by the java command (which is something that interprets compiled Java classes, the mentioned "Java virtual machine"), is the function in your Java class named public static void main. Without such a function, you cannot run the class.
H:\>mkdir MYJAVAAt the DOS prompt where you type normally the java and javac commands, your "working directory" must be the one containing your Java and class files. You change into your directory with the cd command in DOS, which will then become part of your prompt so you know where you are, as in
H:\>cd MYJAVA H:\MYJAVA>(awaiting your next command on the second line shown here).
Suppose you want to use two classes in Java, one called class Stack, the other one, as in Exercise 5, called class Calc that makes use of class Stack.
There are two possibilities:
javac Stack.javaso that the class file Stack.class is found when compiling later the file Calc.java. On newer compilers, this should happen automatically when compiling Calc.java.
public class Stackwhich then has to agree with the filename (that is, the file must be called Stack.java).