Java Garage
Authors: Hewitt E.
Published year: 2006
Pages: 15-17/228
Buy this book on amazon.com >>
     

Running Programs

After you have compiled your source files into bytecode using the javac 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.

Run the java command, passing it the fully qualified name of your class (including all of the package names ).

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:



C:\garage\classes>java net.javagarage.demo.MyProgram

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"

     java.lang.NoClassDefFoundError:

MyProgram

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 cd < dirname > command on Windows and Linux. This is often not convenient , however. To remedy the problem, you can set the classpath system variable.

     

Setting the Classpath

If java is still unable to find your program, you might have to change your CLASSPATH variable. The CLASSPATH variable is where the java program looks to find classes to include when it executes.

In your CLASSPATH setting, make sure that . is included, to indicate the present working directory, as well as any custom directory you make to store class files, such as C:\garage\classes.

In Windows 2000, set the CLASSPATH like this:

  1. Click Start > Settings > Control Panel > System > Advanced Tab > Environment Variables.

  2. Find the one called CLASSPATH (or make it if it doesn't exist) and add the preceding directories to it.

  3. Create this variable as a system variable if it does not exist. Also, make sure that the JAVA_HOME location correctly points to the top-level directory of your Java installation. This is probably C:\Program Files\Java\J2SDK1.5.0 on Windows.

     

Setting the Classpath at Compile Time

You can set the classpath at compile time. This is useful if you have a quick class you want to try out, and it relies on a library or other class that is not currently on your classpath. You do so using the -cp flag. You can also use -classpath if you are exceptionally fond of typing.

Try it like this:



javac -cp C:\garage\classes SomeClass.java

Note that you can combine flags as well:



javac -source 1.5 -cp

     /user/eben/garage/SomeClass.java

Java Garage
Authors: Hewitt E.
Published year: 2006
Pages: 15-17/228
Buy this book on amazon.com >>