Running a Java Program From the command prompt
I suggest using Textpad to edit your Java source code.
- Create a directory (folder) on your root drive (typically the C drive) named csc211.
- Using an editor such as Textpad enter in the Welcome program discussed in class.
- Save as Welcome.java to directory csc211.
- It is a coding standard to make all java classes begin with uppercase. You must follow that standard. Since the name of the class begins with uppercase your file name must begin with uppercase. The filename name must be identical (in case) to the class name.
- Go to the command prompt: (For windows)
- Click the start button
- Select run
- Enter
cmd
orcommand
(depending on your OS) in the combo box labeled open. - You should now see the terminal window. You may see something similar to this at the bottom of the window.
C:\Documents and Settings\Administrator>
Some DOS commands
cd .. takes you back one directory (do this until you reach the root on my computer that would be the C drive ) cd \ takes you to the root. cd csc211 (changes to directory csc211) dir /p (displays names of sub directories / files )
cd \ cd csc211
C:\csc211>
javac Welcome.java
javac is the name of the compiler. If all goes well, you should see a file named Welcome.class in the csc211 directory
dir /p
If this does not compile, check your syntax. If the syntax is ok and you see a message similar to
bad command or javac is not recognizedYou will have to set your PATH variable. You cannot continue until your path variable is set properly. See Java Setup at the resource page.
java Welcome
java WelcomeYou see something similar to :
Exception in thread main ........You will have to update your classpath. See setting classpath at the resource section. For now try entering the command.
java -cp . WelcomeNotice the dot '.'