©1994-2003 Kevin Boone
Home     Section index     K-Zone home Software development

Site search

Glossary
Confused by computer jargon? Look it up!

Shameless plug


Now available!

Articles
- Ten-minute guide to setting up a WAP site

- Talk like your boss: new developments in managerese

More...

Development
File handling in the Linux kernel

Java development for the Sony-Ericsson P800

SunONE Application Server 7 FAQ

More...

Linux
Using Linux with the Treo 600

- Linux on the Tecra M1

- Some notes on openzaurus

More...

Download
Java stuff

Linux stuff

More...

(Please read the download policy)

Home automation
The X10 system

Linux TW723 driver

More...

The K-Zone
K-Zone computing

K-Zone law

K-Zone education and science

K-Zone motorcycles

K-Zone DIY

K-Zone railways

K-Zone martial arts

About the author

K-Zone home page

 
Computing
Java on the P800
J2ME FAQ
Perl for Java programmers
Web Service tutorial
CLASSPATH
EJBs on jBoss
CMP with jBoss
Bean managed persistence
C++ tutorial
Simple Java
JDK setup
Howto-appletviewer
EJB lifecycles
EJB transactions
Inside the J2EE RI
S1AS7 FAQ
iAS 6.0 FAQ
iAS JMQ
How to use the DOS prompt to interact with Sun JDK tools

About this document

This document explains -- very briefly -- how to use the `DOS prompt' to carry out basic file and directory management operations. Specifically it explains how to enter, compile and run Java programs this way. This document was written primarily for people who use Microsoft Windows systems, and are unfamiliar with the use of command-line tools for compiling and running programs.

What is the DOS prompt?

The DOS prompt (or command prompt, or command line) is the standard way of allowing Windows users to interact with their systems by typing commands (rather than clicking and pointing). Although few people want to interact with their software this way, there are occasions where it is helpful, or even essential to do so. An example of the latter is the use of the Sun JDK java tools. These are all designed to be used this way. The widespread uptake of Microsoft Windows has de-skilled many students of this basic technique. It should be remember that there is nothing old-fashioned or backward about using a command prompt, and many professional programmers work this way by choice. The problem is that the command prompt available on Windows systems is extremely rudimentary compared to those on, say, Unix systems, and therefore not much fun to operate. Please bear in mind also that although the term `DOS prompt' is widely used, it is a misnomer. On Windows NT systems no `DOS' is involved at all, but the name has been retained for historical reasons.

Why should I use it?

You should use the command prompt if you need to do a job that is quicker that way, or if your software only has this kind of interface. For example, consider a directory containing 2,000 files, from which you want to delete all files whose names end in `.tmp'. A command to do this is
del *.tmp
which requires 10 keystrokes. There is a way to get the same effect using the Windows `Explorer' program, but it will take much longer.

However, the main reason for using the command prompt is to operate software that can only be used this way. This includes the Sun JDK tools. Remember that this software is designed for professional programmers; on the whole these people will not be put off by the need to use the command prompt.

Although it is not usual to do so, all Windows programs can be activated and, to a certain extent, controlled by the command prompt.

Getting the command prompt

The method for gaining access to the command prompt differs from one system to another. First, look for an icon labelled `DOS prompt' or `command prompt'. Alternatively look in the `Start' menu for something with this name. If this fails, do the following.
  • Right-click on the desktop and select `New', then `Shortcut'
  • In the box that appears enter `cmd' (Windows NT systems) or `command' (other Windows versions)
  • If asked for the name of the shortcut, enter `Command prompt' (some systems will supply that part automatically).
This process will create an icon on the desktop giving access to the command prompt. Alternatively, select `Run' from the `Start' menu, and enter `cmd' or `command' in the box that appears. You will know it is working when you can see the traditional `black screen' with a prompt of the form
c:\windows> _

Basic principles

All commands that you will need to enter have the following basic form:
command_name argument1 argument2...
The odd word `argument' is the term used for anything that is not a command name. The number of these arguments depends on the command; some require none at all, and some can accept a variable number. In most cases the arguments will be the names of the files that the command is to process. Note that command names and arguments are separated by spaces.

For example, the command

copy Prog1.java Prog1.java.bak
Makes a copy of the file `Prog1.java' called `Prog1.java.bak'. Here `copy' is the command name, and the arguments are the two filenames. The prompt itself displays the current working directory. This is the directory in which commands will be looked for first, if no other information is given. In the example above, the command `copy' would look for the file `Prog1.java' in the current directory, whatever that happened to be. The file `prog1.java.bak' will be created in the current directory as well.

Where do the commands come from?

There are two sorts of commands available in Windows systems
  • Intrinsic commands. These are understood by the command prompt itself. Examples include copy, delete, rename.
  • Extrinsic commands. These are programs that are supplied by program files somewhere in the system. The JDK tools come into this category.
For extrinsic commands, the command prompt must be able to find the files that supply the commands. How does it do this? The system has the concept of a `search path'. This is a list of directories (folders) which will be searched each time the user enters a command. The list of directories is search one at a time until either the program is found, or we run out of directories. In this case we will get an error message like:
fred is not recognized as an internal or external command, not the name of a
file on this system
or
Bad command or filename
This message varies from system to system.

You can find the setting of the current path by typing the command

path
It will display something like this:
C:\WINDOWS;C:\WINDOWS\COMMAND;C:\
This means that the command prompt will first look in the directory C:\WINDOWS, then in C:\WINDOWS\COMMAND, then in C:\. You can change the path by typing
path=[something]
but this change will only last while the command prompt is running. If you start a new command prompt the path will be reset to its original value. If you want to make a permanent change to the path you need to edit the system configuation or start-up files. This process is explained in how to set up the Sun JDK. If you have installed the JDK tools correctly, the path should display the directory where the JDK program files are installed, something like this:
C:\WINDOWS;C:\WINDOWS\COMMAND;C:\;C:\JDK1.2.2\BIN
If I type
notepad
at the prompt, it will start the Windows Notepad program, because it finds the program `notepad.exe' in the directory C:\WINDOWS.

Changing directories and drives

The command `cd' changes the current working directory. After executing this command you should see the prompt change to show the new directory name. The general form is
cd directory_name
If the directory names starts with the character `/', this indicates that the directory is specified from the top level directory of the current drive. For example,
cd \windows 
will change to the directory `windows' which is at the top level. The comamnd
cd week1 
will change to the directory `week1', which is assumed to be a sub-directory of the current directory (whatever that happens to be). After executing the command, the prompt wil change to show the new current directory.

To change between disk drives, rather than directories, use the command

X:
where X is replaced with the letter of the drive you want. On some systems you can use `cd' to do this as well.

Note that `cd' does not create or otherwise modify any directories or files; it merely changes the location where subsequent commands will look for files.

Creating and deleting directories

Create a directory by typing
md directory_name
The same considerations apply here as for `cd': if the directory name starts with a `\' is is relative to the top level directory, otherwise it is a subdirectory of the current directory. To create a new directory and then change to that directory, use
md directory_name
cd directory_name
All subsequent commands will look for files in your new directory. Note that this operation is almost the same as selecting `New, Folder' in the Explorer program, then clicking on the name of the new directory.

A directory can be deleted using the command

rd directory_name
but it will only work on directories that contain no files.

Showing, creating and deleting files

Get a list of files in the current directory by typing
dir
Note that this is more authoratative than the file listings in Explorer, because the dir command will show filenames exactly. It will not hide file extensions nor hide files whose names end in `dll' nor any of the other daft things that Explorer does (you can stop Explorer doing this as well, and if you are programming you should certainly find out how to do this).

Files can be deleted with the command

del file
Note that multiple files can be deleted in one go, by specifying a `*' as part of the filename. The * matches any part of a filename. So for example,
del *.java 
deletes all files whose names end in `.java'. This is a powerful technique and needs to be used with caution:
del *
deletes all files in the current directory. Do this in your \WINDOWS directory and you will need to re-install Windows, which is a drag.

There is no specific way to create files; this is the job of the software you are using. For example, to create a file `Test.java' using Notepad, enter

notepad test.java
The file will be created in the current directory.

Java comamnds

To compile a java program which is stored in a file called `Test.java', enter:
javac Test.java
This will only work if (1) the file Test.java is in the current directory, and (2) the JDK tools have been set up correctly. In the event of (1) not being true, you can use `cd' to change the working directory to wherever your Java program is stored. The compiler will produce one or more files whose names in `.class'. There will be one for each Java class defined in the program. Note that the input to `javac' is a filename (or several filenames).

To run a Java program that is a stand-alone application, enter

java class_name
where class_name is replaced by the name of the class that contains the `main' method. For example, if the program I compiled defined only one class, called `Test', I would enter
java Test 
Note that the Java tools are case-sensitive in filename. For example,
java Test 
is a different thing to
java test 
This is because the Java language itself is case-sensitive. Most other commands are not case-sensitive, either in their own names or the names of the files they are operating on. This means that you can't have two files in the same directory whose names are `test.java' and `Test.java' as the system would not be able to tell them apart.

Unlike the other commands I have explained, `java' does not require a filename, but a class name. It is an error to enter

java Test.java 
or even
java Test.class 
Even though it is Test.class that is actuall read by the `java' program.

To show a Java applet using the applet viewer, enter

appletviewer html_file 
where html_file is the name of the HTML file that loads the applet. This process is explained in more detail in how to use the JDK applet viewer.

Further information

There is plenty of information about the command prompt and about commands on the World-Wide Web. Try a Web search for `DOS comamnd reference'. Most commands respond to the following help request
command_name /? 
by displaying a summary of their operation, but this can be very daunting for non-specialists. On some systems, the command
help
displays a list of commands that are available.