After you have compiled your source files into bytecode using the
command, you are ready to execute them. To do so, you need to follow these directions.
Navigate to the directory that stores your top-level package. For the examples in this book, that is a directory called C:\garage\classes. If you are using an IDE, your classes directory might be different. For example, an Eclipse project might store files at C:\eclipse\workspace\garage\classes. In any case, it is the directory that contains your packages, and where you specify the compiler to output code.
command, passing it the fully qualified
of your class (including all of the package
Make sure to use the . separator for packages ”not your system file separator (/ on Linux and \ on Windows). This is a common mistake, because you have to supply the file separator when compiling.
For example, here is how to run the CommonFileTasks.class program:
Make sure that you leave off the .class extension. That can be hard to remember because you have to include the .java extension when compiling.
Make sure that the class you invoke on the command line is the one that contains your
public static void main(String...args)
method. This is the method that is called when the JVM starts your program, and it will tell you if it cannot be found.
If you see this error,
Exception in thread "main"
it means that the java runtime program cannot find your bytecode file, MyProgram.class.
The Java runtime will look for your bytecode file in the present working directory. So if your .class file is in C:\garage\classes, change to that directory using the
command on Windows and Linux. This is often not
, however. To remedy the problem, you can set the classpath system variable.