Wednesday, August 13, 2014

Basic java, javac, javadoc, jar command line options

A JDK provides several command-line tools that aid in software development. Commonly used tools include the compiler, launcher/interpreter, archiver, and documenter. In this article, you will get used to basic command options that java command line tools use. May be you are interested in reading how java program works before reading this command line tools options.



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. Here is a list of commonly used compiler options:

javac [-options] [source files]
Compiles Java source files.

  • javac HelloWorld.java
    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 (e.g., /opt/hwapp/classes/com/oreilly/tutorial/).
  • 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 (e.g., /opt/hwapp/src/com/oreilly/tutorial/).
  • 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 –X
    The –X option prints a synopsis of nonstandard options. For example, –Xlint:unchecked enables recommended warnings, which prints out further details for unchecked or unsafe operations.
  • 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.

Tip :  Even though –Xlint and other -X options are commonly found among Java compilers, the –X options are not standardized, so their availability across JDKs should not be assumed.


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

java [-options] class [arguments...]
Runs the interpreter.
  • java HelloWorld
    Starts the JRE, loads the class HelloWorld, and runs the main method of the class.
  • java com.java.latte.HelloWorld
    Starts the JRE, loads the HelloWorld class under the com/java/latte/ directory, and runs the main method of the class.
  • java -cp /tmp/Classes HelloWorld
    The –cp and –classpath options identify classpath directories to use at runtime.
  • java –Dsun.java2d.ddscale=true HelloWorld
    The –D option sets a system property value. Here, 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
  • java -da HelloWorld
    The –da and –disableassertions options disable Java assertions.
  • java –client HelloWorld
    The –client option selects the client virtual machine to enhance interactive applications such as GUIs.
  • java –server HelloWorld
    The –server option selects the server 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 [-d32 | -d64]
    The [-d32] and the [-d64] options call for the use of the 32-bit or the 64-bit data model (respectively), if available.
  • 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> &


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). Here 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/java/latte/ 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/java/latte/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.

Tip :  Several other ZIP tools (e.g., 7-Zip, WinZip, and Win-RAR) can work with JAR files.


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. Create a HelloWorld.java file.
  2. Create the subfolders com/java/latte/
  3. Run javac HelloWorld.
    Use this command to compile the program and place the HelloWorld.class file into the com/java/latte directory.
  4. Create a file named 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.java.latte.HelloWorld
  5. Run jar cmf Manifest.txt helloWorld.jar com/java/latte.
    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.7.0 (Oracle Corporation)
    Main-Class: com.java.latte.HelloWorld
  6. Run jar tf HelloWorld.jar.
    Use this command to display the contents of the JAR file:
    META-INF/
    META-INF/MANIFEST.MF
    com/
    com/java/
    com/java/latte
    com/java/latte/HelloWorld.class
  7. Finally, run java –jar HelloWorld.jar.
    Use this command to execute the JAR file.


Classpath
The classpath is an argument set used by several command-line tools that tells the JVM where to look for user-defined classes and packages. Classpath conventions differ among operating systems.

On Microsoft Windows, directories within paths are delineated with backslashes, and the semicolon is used to separate the paths:
-classpath \home\XClasses\;dir\YClasses\;.

On POSIX-compliant operations systems (e.g., Solaris, Linux,and Mac OS X), directories within paths are delineated with forward slashes and the colon is used to separate the paths:
-classpath /home/XClasses/:dir/YClasses/:.

The CLASSPATH environmental variable can also be set to tell the Java compiler where to look for class files and packages


Java Documenter
Javadoc is a command-line tool used to generate documentationon source files. The documentation is more detailed when the appropriate Javadoc comments are applied to the source code.

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, allclasses-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 in 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.


If you know anyone who has started learning Java, why not help them out! Just share this post with them. Thanks for studying today!...

1 comment: