Wednesday, October 28, 2009

Section 10.4. Command-Line Tools







10.4. Command-Line Tools

A JDK provides several command-line tools that aid in software development. Commonly used tools include the compiler, launcher/interpreter, archiver, and documenter. Find a complete list of tools at http://java.sun.com/javase/6/docs/technotes/tools/.

10.4.1. Java Compiler

The Java compiler translates Java source files into Java bytecode. The compiler creates a bytecode file with the same name as the source file but with the .class extension. Following is a list of commonly used compiler options.


javac [-options] [source files]

This is the usage to compile Java source files.


javac HelloWorld.java

This basic usage compiles the program to produce HelloWorld.class.


javac –cp /dir/Classes/ HelloWorld.java

The –cp and –classpath options are equivalent and identify classpath directories to utilize at compile time.


javac –d /opt/hwapp/classes HelloWorld.java

The –d option places generated class files into a preexisting specified directory. If there is a package definition, the path will be included (i.e., /opt/hwapp/src/com/oreilly/ utils/).


javac –s /opt/hwapp/src HelloWorld.java

The –s option places generated source files into a preexisting specified directory. If there is a package definition, the path will be further expanded (i.e., /opt/hwapp/ src/com/oreilly/utils/).


javac –source 1.4 HelloWorld.java

The –source option provides source compatibility with the given release, allowing unsupported keywords to be used as identifiers.


javac –Xlint:unchecked

The –X[lint] option enables recommended warnings. This example prints out further details for unchecked or unsafe operations.

Even though the –Xlint option is commonly found among Java compilers, the –X options are not standardized.



javac –version

The –version option prints the version of the javac utility.


javac –help

The –help option, or the absence of arguments, will cause the help information for the javac command to be printed.

10.4.2. Java Interpreter

The Java interpreter handles the program execution, including launching the application. Following is a list of commonly used interpreter options.


java [-options] class [arguments…]

This is the usage to run the interpreter.


java [-options] –jar jarfile [arguments…]

This is the usage to execute a JAR file.


java HelloWorld

This basic usage starts the JRE, loads the class HelloWorld, and runs the main method of the class.


java com.oreilly.utils.HelloWorld

This basic usage starts the JRE, loads the HelloWorld class under the com/oreilly/utils/ directory, and runs the main method of the class.


java -cp /tmp/Classes HelloWorld

The –cp and –classpath options identify classpath directories to utilize at runtime.


java –Dsun.java2d.ddscale=true HelloWorld

The –D options sets a system property value. Here, the hardware accelerator scaling is turned on.


java –ea HelloWorld

The –ea and –enableassertions options enable Java assertions. Assertions are diagnostic code that you insert in your application. For more information on assertions, see the "Assert Statement" section in Chapter 6.


java -da HelloWorld

The –da and –disableassertions options disable Java assertions.


java –client HelloWorld

The –client option selects the client virtual machine (versus the server virtual machine) to enhance interactive applications such as GUIs.


java –server HelloWorld

The –server option selects the server virtual machine (versus the client virtual machine) to enhance overall system performance.


java –splash:images/world.gif HelloWorld

The –splash option accepts an argument to display a splash screen of an image prior to running the application.


java –version

The –version option prints the version of the Java interpreter, the JRE, and the virtual machine.


java –help

The –help option, or the absence of arguments, will cause the help information for the java command to be printed.


javaw <classname>

On the Windows OS, javaw is equivalent to the java command but without a console window. The Linux equivalent is accomplished by running the java command as a background process with the ampersand, java <classname> &.

10.4.3. Java Program Packager

The Java Archive (JAR) utility is an archiving and compression tool, typically used to combine multiple files into a single file called a JAR file. JAR consists of a ZIP archive containing a manifest file (JAR content describer) and optional signature files (for security). Following is a list of commonly used JAR options along with examples.


jar [options] [jar-file] [manifest-files] [entry-point] [-C dir] files…

This is the usage for the JAR utility.


jar cf files.jar HelloWorld.java com/oreilly/utils/HelloWorld.class

The c option allows for the creation of a JAR file. The f option allows for the specification of the filename. In this example, HelloWorld.java and com/oreilly/utils/HelloWorld.class are included in the JAR file.


jar tfv files.jar

The t option is used to list the table of contents of the JAR file. The f option is used to specify the filename. The v option lists the contents in verbose format.


jar xf files.jar

The x option allows for the extraction of the contents of the JAR file. The f option allows for the specification of the filename.

Several other ZIP tools, such as WinZip® and Win- RAR®, can work with JAR files.


10.4.4. JAR File Execution

JAR files can be created so that they are executable by specifying the file within the JAR where the "main" class resides, so the Java interpreter knows which main( ) method to utilize. Here is a complete example of making a JAR file executable.

  1. javac HelloWorld

    Use this command to compile the program and place the HelloWorld.class file into the com/oreilly/utils directory.

  2. Create a file Manifest.txt in the directory where the package is located. In the file, include the following line specifying where the main class is located:

    	Main-Class: com.oreilly.utils.HelloWorld


  3. jar cmf Manifest.txt helloWorld.jar com/oreilly/utils

    Use this command to create a JAR file that adds the Manifest.txt contents to the manifest file, MANIFEST.MF. The manifest file is also used to define extensions and various package-related data.

    	Manifest-Version: 1.0
    Created-By: 1.6.0 (Sun Microsystems Inc.)
    Main-Class: com.oreilly.utils.HelloWorld


  4. jar tf HelloWorld.jar

    Use this command to display the contents of the JAR file.

    	META-INF/
    META-INF/MANIFEST.MF
    com/
    com/oreilly/
    com/oreilly/utils
    com/oreilly/utils/HelloWorld.class


  5. java –jar HelloWorld.jar

    Use this command to execute the JAR file.

10.4.5. Java Documenter

Javadoc is a command-line tool used to generate documentation on source files. The documentation is more detailed when the appropriate Javadoc comments are applied to the source code; see the "Comments" section in Chapter 2. Here is a list of commonly used javadoc options and examples.


javadoc [options] [packagenames] [sourcefiles]

This is the usage to produce Java documentation.


javadoc HelloWorld.java

The javadoc command generates HTML documentation files: HelloWorld.html, index.html, allclaases-frame.html, constant-values.html, deprecated-list.html, overview-tree.html, package-summary.html, etc.


javadoc –verbose HelloWorld.java

The –verbose option provides more details while Javadoc is running.


javadoc –d /tmp/ HelloWorld.java

This –d option specifies the directory where the generated HTML files will be extracted to. Without this option, the files will be placed into the current working directory.


javadoc –sourcespath /Classes/ Test.java

The –sourcepath option specifies where to find user .java source files.


javadoc –exclude <pkglist> Test.java

The –exclude option specifies which packages not to generate HTML documentation files for.


javadoc –public Test.java

The –public option produces documentation for public classes and members.


javadoc –protected Test.java

The –protected option produces documentation for protected and public classes and members. This is the default setting.


javadoc –package Test.java

The –package option produces documentation for package, protected, and public classes and members.


javadoc –private Test.java

The –private option produces documentation for all classes and members.


javadoc –help

The –help option, or the absence of arguments, causes the help information for the javadoc command to be printed.








No comments:

Post a Comment