Click here to learn how to install a JDK.
These instructions assume that you are familiar with a command-line system
such as DOS (also known as the Command Prompt window for Windows), a Linux or
Unix "shell", or the Macintosh terminal
- Connect to the folder containing a Java program you'd
like to run. If you want to create or edit a ".java" file,
use your favorite text editor.
- To compile a Java class, use the javac command (stands for
"java compiler"). If your class is called MyClass, it would
be in the file MyClass.java and you would type javac MyClass.java.
If there are errors, you'll see them on the screen under your command.
If there are no errors, you'll just get a new command prompt after a
short pause.
- The javac program is pretty clever about compilation order.
When you tell it to compile a class, it actually compiles not only that
class but also any classes used by that class which need to be recompiled
(i.e. the .java file is newer than the .class file). So
if you've got a program with many classes, you don't need to compile each
one separately. Just ask javac to compile the main
class and it will take care of everything.
- Once your program is compiled, use the java command to run
it. If MyClass is the main class in your program, type
java MyClass. After a short pause, you will see the output
of your program on the screen under your command.
- The picky thing you have to remember is that with javac you
must type ".java" after the class name. With java,
you use the class name by itself (no .class).
Frequently Asked Question: When I try step 2 above with the command prompt on
a Windows machine, I get this error message:
'javac' is not recognized as an internal or external command,
operable program or batch file. What's going on?
Answer: This error message could mean that you haven't installed a JDK on your
computer. But it could also just mean that DOS or Linux just can't find it. Find the
directory that contains the javac and java commands and add it to your
PATH variable. If you're running Windows and don't know how to change your
path, do a Google for "Windows environment variables", adding the name of the Windows
version that you're running. You'll find many good explanations.
This page maintained by
Margaret Lamb,
Queen's University, Kingston, Ontario. Last modified