Home: www.vipan.com Vipan Singla e-mail: vipan@vipan.com
Typical Java Commands

How To Find Out Java's System Environment - Example Java Code

javac - Java Compiler

  • Compile .java file, place the .class file in the same directory as the .java file
    javac Foo.java
  • Display what you are doing (-verbose) (usually unnecessary). Add debugging info in .class file (-g) (slower, adds size)
    javac -verbose -g Foo.java
  • Compile many .java files
    javac Foo.java Foohoo.java Foo2.java
    javac *.java
  • Compile a lot of .java files listed in a text file, one file name per line. e.g. "manyfiles" contains:
    Foo.java
    Foohoo.java
    Foo2.java
    
    javac @manyfiles

javac Search Order For .class Files

javac Search Order For .java Files If -sourcepath Is Not Specified

javac Search Order For .java Files If -sourcepath Is Specified

java - Run Java programs

  • The .java file which must have the following method:
    public static void main(String args[]){ . . . }
  • Since "main" method is always static, it cannot use non-static variables which is a severe limitation. (Each variable declared outside the "main" method has to be declared "static".) As a work-around, instantiate a new object of the same class which contains the "main" method. All non-static variables will then become available to "main" method because a particular instance of the class now exists in memory.
    e.g., for Foo.java, use:
    
    public class Foo
    {
       private static Foo shark;
       public static void main(String args[])
          {shark = new Foo(); shark.init(); System.exit(0); }
       public void init(){  //Your Application code here . . . }
    }
       
    (The "init" method also doubles as the default applet starting method in case the application needs to be converted to an applet.)

jar - Compress, Bundle-Up, and Extract Files

  • Create a new .jar file which includes all files in the current working directory (Specify the .jar extension, it is not added automatically)
    jar cvf myProgram.jar .
    jar cvf myProgram.jar *
  • The "META-INF/MANIFEST.MF" manifest text file is automatically created and included in the .jar file. By default, the manifest file contains only one line specifying the version number of the .jar file.
  • Create a new .jar file which includes specific files in the current working directory
    jar cvf myProgram.jar *.class Foo.java ./photo.gif
  • Temporarily change the current working dircetory before processing files ("*" does not work. Use "." to include all files and directories.)
    jar cvf myProgram.jar -C /junk/SomeDir .
    -C seems buggy in Windows.

appletviewer - to view applets without a browser

javadoc - Automatic Java Documentation Generator

keytool - Generate And Manage Security Keys

jarsigner - Sign .jar Files Or Verify Their Signatures


© 2000 Vipan Singla