Apr 7, 2012



After saving your source code with the extension .java, you can compile it using the Java compiler, javac. To run the javac compiler, execute it with the following syntax. (Note that javac expects an extension after the file name.)

    javac [ options ] filename.java
    javac_g [ options ] filename.java

For your example, run the following command:

    javac HelloWorld.java

If the code compiles correctly, you will see two files in your classes directory: HelloWorld.java and HelloWorld.class. The .class file has now been converted to bytecode.

The following error message might result if you mistyped the javac commands. Retype the command carefully if you receive this error:

    For Windows NT/95:  bad command or file name
    For Solaris:  /bin/sh: javac: not found

The next error means that an expression is mistyped in your source code. Check your source code for errors if you receive this message:

    Invalid expression statement

The following error means that either the javac command or your Java file cannot be found. If you receive this error, make sure that your path includes the directory containing the Java tools so you are able to run the tools from any directory without an explicit mapping to them. Also, make sure you are running the command from the same directory as your HelloWorld.java file.

Error message for Windows NT/95:

    Bad command or file name

Error message for Solaris:

    /bin/sh: javac: not found

When you run the compiler, you can feed it certain options that change its behavior. Below Table provides a list of all of the command-line options that you can feed javac and a description of each option.

-classpath path
Sets path where javac looks for classes it needs. Directory list is colon-delimited.
-d directory
Specifies the root directory for creating a directory tree for a hierarchy of packages.
Turns on debugging tables in code generation for later debugging of bytecodes.
Suppresses warnings that the compiler produces.
Optimizes code produced by inlining static, final, and private methods.
Prints messages about the source file and classes.

javac_g is a non-optimized version of javac that is suitable for use with debuggers such as jdb.


Post a Comment