Indiana University Southeast

advertisement
Indiana University Southeast
CSCI – C 202 - Introduction to Software Systems
INFO – I 211 – Information Infrastructure II
Fall 2008
The purpose of this handout is to familiarize you with using the Command Prompt and notepad to
develop, compile and run your java lab applications.
To create your source files, enter your code into notepad, and save your class file as a “.java “ file.


The file must have the same name as the java class name in your class declaration, and
it must have the .java extension.
To ensure this, when you use “Sava As” in notepad, enclose your filename and the
extension in double quotes: for example “MyJavaClass.java”.
To run and compile your java class, you will use the Command Prompt. To open it, click on start->all
programs->accessories->command prompt. Alternatively, you can click on start->run and type “cmd” in
the dialogue box. Once the command prompt window is open, you will need to navigate to the directory
where your source files are saved (notes on how to navigate though directories are included at the end
of this document).
From your project directory, typing the command “javac” at the command prompt, followed by your
class file name, will compile your source file. Once the source file is compiled, the “java” command
followed by your class name will execute it: eg. “javac MyJavaClass” to compile, and “java MyJavaClass”
to run.
In order for you to run these commands, however, you will need to tell the computer how to execute
the javac and java commands. To do this, you need to create .bat files, called “batch” files.
Using notepad, create a “java.bat” and “javac.bat” and keep them in the same folder as your source
files. As long as you have those .bat files in your project folder, your computer will know where to look
to execute the “javac” and “java” commands.

To create the java.bat file, type into notepad the following (the double quotes are necessary).
Then save the file as “java.bat” enclosed in double quotes: (In Vista you don’t need the quotes)
set classpath=
"C:\Program Files\Java\jdk1.6.0_07\bin\java" %1

Here is the information necessary for the javac.bat file. Save it as “javac.bat”.
set classpath=
"C:\Program Files\Java\jdk1.6.0_02\bin\javac" %1.java
Note: the specific path to the correct java execution files may be different on your computer, especially
the specific java version ( in this case the “jdk1.6.0_02” portion). You may have to browse your
C:\Program Files\Java directory to find the exact version numbers.
Navigating directories using the Command Prompt (MS Console Window)
When you first open your command window, it will probably start you off in a directory like: C:\...\My
Documents. If that’s where your source files are, then you are ok. If not, you’ll need to navigate to the
correct directory (perhaps to a folder on you flash drive).
Here are some simple commands to help you on your way.
“Change Directory” Commands to Navigate Around the Computer
cd\
cd..
cd directory
always bring you back to the root directory (ie. the drive name)
takes you up one directory level
moves you to the specified sub-directory from your current directory;
alternatively you can jump several sub-directories with cd directory/subdirectory1/ sub-directory2/etc.
Changing Drives
If your files are saved on a different drive, you can change to that drive (eg. your flash drive), by
typing the driver letter followed by a colon (for your flash drive, probably E: or F: ).
Other Useful Commands
dir
dir /p
dir *.java
lets you see the directories and files contained within the current directory
lets you view the above contents one page at a time
lets you see just the list of your source files
mkdir myDirectory
rmdir myDirectory
creates a sub-directory called “myDirectory” in the current directory(or md)
removes the directory called “myDirectory” from the current directory(or rd)
del file1
del *.class
del *.class /p
deletes the file named “file1” from the current directory
deletes all the .class files (your compiled .java files) from the directory
deletes the * .class files (your compiled .java files) from the directory one by one
with prompting
clears the screen, for you to start a fresh in the same window
cls
…and finally…
For more fun, you can always type in “help” for a list of available commands. If you need more help with
a specific command, type “help” followed by a space and the command name.
Download