Using the javac and java Commands

  1.Compiling with javac
    javac [options] [source files]
    Both the [options] and the [source files] are optional parts of the command, and both allow multiple entries.
    eg: javac -help
          javac -classpath com:. -g Foo.java Bar.java
  1.1 Compiling with -d
        The -d option lets you tell the compiler in which directory to put the .class file(s) it generates (d is for destination). 
        eg:
        myProject
               |
               |–source
               |      |
               |      |–com
               |           |
               |           |–wickedlysmart
               |                       |
               |                       |–MyClass.java
               |–classes
               |      |
               |      |–com
               |           |
               |           |–wickedlysmart
               |                       |
               |                       |– (MyClass.class goes here)

               javac -d ../classes com/wickedlysmart/MyClass.java (current directory is source,  “../” means back to source directory’s
                  parent directory)
              Somewhat amazingly, the javac command can sometimes help you out by building directories it needs(for this example , if
                  your only have the “classes” directory, javac command will help you build the “com/wickedlysmart” directory,but you must
                  have the “classes directory or you will have an compiling error”)

  2. Launching Applications with java
        java [options] class [args]
        2.1 Using System Properties (use -D option)
        eg: java -DcmdProp=”cmdVal take 2″ TestProps 1 2
        cmdProp will be a system properties that have a value of “cmdVal take 2”,can use System.getProperties() to get it;

  3. Searching for Other Classes  

    Both java and javac use the same basic search algorithm:

  1. They both have the same list of places (directories) they search, to look for classes.

  2. They both search through this list of directories in the same order.

  3. As soon as they find the class they’re looking for, they stop searching for that class. In the case that their search lists contain two or more files with the same name, the first file found will be the file that is used.

  4. The first place they look is in the directories that contain the classes that come standard with J2SE.

  5. The second place they look is in the directories defined by classpaths.

  6. Classpaths should be thought of as “class search paths.” They are lists of directories in which classes might be found.

  7. There are two places where classpaths can be declared:

    1. A classpath can be declared as an operating system environment variable. The classpath declared here is used by default, whenever java or javac are invoked.

    2. A classpath can be declared as a command-line option for either java or javac. Classpaths declared as command-line options override the classpath declared as an environment variable, but they persist only for the length of the invocation.

    3.1  Declaring and Using Classpaths
           eg: -classpath /com/foo/acct:/com/foo
           Classpaths consist of a variable number of directory locations, separated by delimiters. For Unix-based operating systems, 
           forward slashes are used to construct directory locations, and the separator is the colon (:). In Windows ,your directories will
           be declared using backslashes (\) and the separator character you use will be a semicolon (;). 
           It’s also important to remember that classpaths are searched from left to right. Therefore in a situation where classes with 
           duplicate names are located in several different directories in the following classpaths, different results will occur.
           By default java command will not search the directory which the .class file is in, it must add dot(.) to the classpath,eg:
           -classpath /com/foo/acct:/com/foo:.
    3.2 Relative and Absolute Paths
           eg : -cp dirB:dirB/dirC 
                  -cp /dirB:/dirA/dirB/dirC
                  the java command allows you to abbreviate -classpath with -cp.
                  the first one is a relative class path , it will search current directory’s child directory “dirB” and dirB’s child DIR “dirC”,
                  but it will not search current directory.
                  the second is a absolute class path, the base directory is root directory(here is in unix-based system,if in windows
                  there would be like “c:\dirB”). No matter what the current directory is , it will search “/dirB” and “/dirA/dirB/dirC”.

Leave a Reply

Your email address will not be published. Required fields are marked *

You must enable javascript to see captcha here!