Java Tool Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 4.12, 2006

'java' - The Java Launcher

Part:   1  2  3   4 

Java Tool Tutorials

© 2006 Dr. Herong Yang

Latest updates:

  'javac' - The Java Compiler

  'java' - The Java Launcher

  'jdb' - The Java Debugger

  JAR File & 'jar' Tool

  Certificates and 'keytool'

  Installing J2SE 1.5.0

... Table of Contents

(Continued from previous part...)

"-jar" - Executable JAR Files

In order to use the "-jar" option, we need to create a JAR file with a "Main-Class" attribute in the manifest file.

Here is what I did to create a JAR file, and launch it with the "-jar" option.

First I created a manifest file, hello.fs:

Main-Class: Hello

Then I created a JAR file, hello.jar:

>javac Hello.java

>jar -cvmf hello.fs hello.jar Hello.class
added manifest
adding: Hello.class(in = 416) (out= 285)(deflated 31%)

>java -jar hello.jar
Hello world!

"-X" Options to Control Memory Size

"java" can also take "non-standard" options which must be started with "-X".

Two of the "non-standard" options are very useful to control the memory size of the JVM to be launched.

-Xmsn 
-Xmxn

where "ms" specifies the initial size of the JVM, "mx" specifies the maximum size of the JVM, and "n" specifies the memory size in bytes, kilo bytes or mega bytes. For example: -Xms32M and -Xmx64M.

Here is a program to show you how to use these options:

/**
 * ShowMemory.java
 * Copyright (c) 2006 by Dr. Herong Yang, http://www.herongyang.com/
 */
class ShowMemory {
   public static void main(String[] a) {
      Runtime rt = Runtime.getRuntime();
      System.out.println(" Free memory: " + rt.freeMemory());
      System.out.println("Total memory: " + rt.totalMemory());
      while (true);
   }
}

I launched the program with different options:

>javac ShowMemory.java

>java ShowMemory
 Free memory: 1896232
Total memory: 2031616

>java -Xms32M -Xmx128M ShowMemory
 Free memory: 33222440
Total memory: 33357824

>java -Xms64M -Xmx128M ShowMemory
 Free memory: 66514728
Total memory: 66650112

Note that ShowMemory is running an infinite loop at the end. You need to stop the program by "Ctrl-C"

(Continued on next part...)

Part:   1  2  3   4 

Dr. Herong Yang, updated in 2006
Java Tool Tutorials - Herong's Tutorial Notes - 'java' - The Java Launcher