The Command Prompt program allows you to work in an environment that looks more like a traditional operating system as opposed to the icon based Windows environment. In Command Prompt, you will use your keyboard. You won't use your mouse at all. Command Prompt works at a lower level than Windows. This means that you will have more control over the machine. The disadvantage is that it is less user-friendly.
You will need the command prompt in COS 126 to compile and execute your Java programs. Learning the Command Prompt also provides a gradual transition to Unix and Linux systems, which are prevalent in science, engineering, and industry.
To launch Command Prompt select Start -> Run and type cmd in the box.
The Command Prompt shows up as a black terminal window. The command prompt should look something like:
This is where you type commands. The boldface type below (that follows the command prompt) is what you should type as you work through this tutorial. Windows does not care if you use upper or lower case. That means that command cd is the same as CD. It also means that, in Windows, file HelloWorld.java is the same as helloworld.java. This is NOT true in the system to which you will be submitting your files. Be very careful!!!C:\>
Some Useful
Commands
|
- javac: To compile a Java program, use the
javac
command.
Your program
should compile without any errors or warnings (or if there are warnings be
absolutely sure that they do not indicate a flaw in your program).
C:\>javac HelloWorld.java
- java: To run a successfully compiled Java program, use
the java command.
C:\>java HelloWorld
- more: Display the contents of a file one screenful at a time.
C:\>more HelloWorld.java
- exit: Exit the Command Prompt program and close the
terminal window.
C:\>exit
|
- dir: To view the contents of a directory, type
dir. This command will list all the files and directories within
the current directory. It is analogous to clicking on a Windows folder to
see what's inside.
C:\> dir Volume in drive C has no label. Volume Serial Number is C8C7-BDCD Directory of C:\ 10/26/2004 01:36 PM 0 AUTOEXEC.BAT 10/26/2004 01:36 PM 0 CONFIG.SYS 02/10/2005 01:36 PM 126 HelloWorld.java 12/09/2004 12:11 AM DIR Documents and Settings 02/10/2005 08:59 PM DIR introcs 11/02/2004 08:31 PM DIR j2sdk1.4.2_06 12/29/2004 07:15 PM DIR Program Files 01/13/2005 07:33 AM DIR WINDOWS 3 File(s) 126 bytes 5 Dir(s) 32,551,940,096 bytes free
- cd: It is frequently useful to know in which
directory you are currently working. In order to find out, type
cd at the command prompt.
C:\> cd C:\
C:\> cd introcs
C:\introcs>
C:\introcs> dir Volume in drive C has no label. Volume Serial Number is C8C7-BDCD Directory of C:\introcs 02/10/2005 08:59 PM DIR . 02/10/2005 08:59 PM DIR .. 02/03/2005 11:53 PM 126 HelloWorld.java 01/17/2005 01:16 AM 256 readme.txt 2 File(s) 382 bytes 2 Dir(s)
C:\introcs> cd .. C:\>
- mkdir: To create a new directory, use the command mkdir.
The following command creates a directory named hello,
which you can use to to store all of your files associated with
the Hello World assignment.
C:\introcs> mkdir hello
C:\introcs> dir Volume in drive C has no label. Volume Serial Number is C8C7-BDCD Directory of C:\introcs 02/10/2005 08:59 PM DIR . 02/10/2005 08:59 PM DIR .. 02/11/2005 02:53 PM DIR hello 02/03/2005 11:53 PM 126 HelloWorld.java 01/17/2005 01:16 AM 256 readme.txt 2 File(s) 382 bytes 3 Dir(s)
- move: Now, move the two files HelloWorld.java and
readme.txt into the hello directory using the move
command.
C:\introcs> move HelloWorld.java hello C:\introcs> move readme.txt hello C:\introcs> dir Volume in drive C has no label. Volume Serial Number is C8C7-BDCD Directory of C:\introcs 02/10/2005 08:59 PM DIR . 02/10/2005 08:59 PM DIR .. 02/11/2005 02:53 PM DIR hello 0 File(s) 0 bytes 3 Dir(s)
C:\introcs> cd hello C:\introcs\hello> dir Volume in drive C has no label. Volume Serial Number is C8C7-BDCD Directory of C:\introcs\hello 02/10/2005 08:59 PM DIR . 02/10/2005 08:59 PM DIR .. 02/03/2005 11:53 PM 126 HelloWorld.java 01/17/2005 01:16 AM 256 readme.txt 2 File(s) 382 bytes 2 Dir(s)
C:\introcs\hello> dir Volume in drive C has no label. Volume Serial Number is C8C7-BDCD Directory of C:\introcs\hello 02/10/2005 08:59 PM DIR . 02/10/2005 08:59 PM DIR .. 02/03/2005 11:53 PM 126 helloworld.java 01/17/2005 01:16 AM 256 readme.txt 2 File(s) 382 bytes 2 Dir(s) C:\introcs\hello> move helloworld.java temp.java C:\introcs\hello> move temp.java HelloWorld.java C:\introcs\hello> dir Volume in drive C has no label. Volume Serial Number is C8C7-BDCD Directory of C:\introcs\hello 02/10/2005 08:59 PM DIR . 02/10/2005 08:59 PM DIR .. 02/03/2005 11:53 PM 126 HelloWorld.java 01/17/2005 01:16 AM 256 readme.txt 2 File(s) 382 bytes 2 Dir(s)
- copy: To make a copy of a file,
use the copy command. The following command
creates a backup copy of our HelloWorld.java program. This is especially
useful when you
modify a working program, but might want to revert back to the original
version if
your modifications don't succeed.
C:\introcs\hello> copy HelloWorld.java HelloWorld.bak C:\introcs\hello> dir Volume in drive C has no label. Volume Serial Number is C8C7-BDCD Directory of C:\introcs\hello 02/10/2005 08:59 PM DIR . 02/10/2005 08:59 PM DIR .. 02/03/2005 11:53 PM 126 HelloWorld.java 01/17/2005 01:16 AM 256 readme.txt 2 File(s) 382 bytes 3 Dir(s)
- del: Subsequently, you might want to clean up useless
files. The del command deletes a file.
C:\introcs\hello> del HelloWorld.bak C:\introcs\hello> dir Volume in drive C has no label. Volume Serial Number is C8C7-BDCD Directory of C:\introcs 02/10/2005 08:59 PM DIR . 02/10/2005 08:59 PM DIR .. 02/03/2005 11:53 PM 126 HelloWorld.java 01/17/2005 01:16 AM 256 readme.txt 2 File(s) 382 bytes 3 Dir(s)
- wildcards:
You can also apply the copy, del, and move
commands to several files (or directories) at once.
To create a new directory called loops,
and copy all of the files in the hello directory
C:\introcs\hello\
into this newly created directory type:
C:\introcs> mkdir loops C:\introcs> copy c:\introcs\hello\* loops
|
C:\introcs\loops> java CenterofMass 0 0 10 1 1 10 0.5 0.5 20
- Redirecting standard input.
As an alternative, we can create a file
that consists of the same six input numbers.
Using a text editor (like jEdit), create a file named input.txt,
and type in the six numbers.
After saving the file in the loops directory, type the following
command to verify that you
entered the integers correctly:
C:\introcs\loops> more input.txt 0 0 10 1 1 10
C:\introcs\loops> java CenterofMass < input.txt 0.5 0.5 20
- Redirecting standard output. Similarly it is possible to redirect
the output to a file instead of to the screen. Continuing with the same
example, if we want to save the output permanently, we can use the output
redirection symbol '>'.
C:\introcs\loops> java CenterofMass > output.txt 0 0 10 1 1 10
phoenix.Princeton.EDU% more output.txt
- Redirecting standard input and standard output. It is often useful to
use both redirection operations simultaneously.
C:\introcs\loops> java CenterofMass < input.txt > output2.txt
|
Note that more will work by redirecting the file temp.txt to standard input (as is done here) or by simply using the filename (as is done at the beginning of the document). Instead, we could do this in one line using the pipe symbol '|'C:\introcs> java RandInts > temp.txt C:\introcs> more < temp.txt
This is often useful when debugging a program, especially if your program goes into an infinite loop and you want to see the first few values that it prints.C:\introcs> java RandInts | more