Compiling and Running Java Programs


The standard Java compiler is named javac and comes with the SDK. To compile a Java program, you type "javac," followed by any compiler options and the name of the source code to be compiled.

 javac <options> <source_code> 

You can specify more than one source file to be compiled. Table 1.1 lists the Java compiler options and what they do. You can also get this list by typing either "javac" or "javac “help." You will find that for many of your programs, you will simply use the javac keyword by itself without any options.

Table 1.1. Java Compiler Options

O PTION

D ESCRIPTION

-g

Full debugging information

-g:none

No debugging information

-g:{lines,vars,source}

Only some debugging information

-O

Optimize. This option may hinder debugging and/or create larger .class files

-nowarn

Generates no warnings

-verbose

Verbose compiler output

- deprecation

Indicates where deprecated APIs are used

-classpath < path >

Indicates where compiler should look for .class files

-sourcepath <path>

Indicates where compiler should look for source files

-bootclasspath <path>

Overrides location of bootstrap class files

-extdirs <dirs>

Overrides location of installed extensions

-d <directory>

Specifies where .class files will be placed

-encoding <encoding>

Specifies character encoding scheme to be used

-source <release>

Checks source compatibility with specified release

-target <release>

Generates .class files for the specified Virtual Machine

-help

Prints the list of compiler options

The Java compiler is smart in that it will automatically compile any dependent source code. For example, let's say the Blah.java program makes use of source code contained in the Foo.java program. If you compile the Blah.java program by typing "javac Blah.java" the compiler will also, if necessary, compile the Foo.java code.

The output of the Java compiler is the bytecode of the compiled program. The bytecode file name will have the same name of the source code sent to the compiler with the ".class" extension. For example, to compile the Blah .java source code without any compiler options you would type

 javac Blah.java 

This will generate a bytecode file named Blah.class .

To run a Java program, you must send the appropriate bytecode to the JVM. You do this by typing "java," any JVM options, and the .class file name.

 java <options> <class_file> 

You do not type the .class extension. For example, to run the compiled Blah.java program without any JVM options, you would type ”

 java Blah 

Some of the more commonly used standard JVM options are listed in Table 1.2.

Table 1.2. Virtual Machine Options

O PTION

D ESCRIPTION

-client

Specifies the client Virtual Machine

-server

Specifies the server Virtual Machine

-cp <directories>

 

-classpath <directories>

Indicates where to look for classes and resources

-D <name>=<value>

Sets a system property

-verbose[:classgcin]

Enables verbose output

-version

Prints product version and exits

-showversion

Prints product version and continues with other commands

-?

 

-help

Lists standard JVM options

-X

Lists nonstandard JVM options



Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net