Using the javac and java Commands

Posted by Wayne Wan in English Study, Java Foundation on 19-09-2008

Tags: ,

  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”.

Comments:

Hi, admin, can I copy some information from the post?

Thank you for sharing the info. I found the info very helpful.

Good job! Thank you for posting.

I will add this blog to my favorites, it is great.

Perfect!! I find something interesting,.

Waynewan…

[…] something about waynewan[…]…

good articles here hopefully i learned from it.

I really liked this post. You describe this topic very well. I really enjoy reading your blog and I will definetly bookmark it! Keep up the great posts! :)

Finally, I found the information I was searching for. I have been doing research on this subject, and for two days I keep entering web-sites that are supposed to have what I am looking for, only to be disappointed with the lack of what I wanted. I wish I would have found your website quicker! I had about 30% of what I needed and your site has that, and the rest of what I had to have to complete my research. Thank you and I will report back on how it goes!

I love your site.

Thanks for this interesting information.

hey, nice blog…really like it and added to bookmarks. keep up with good work

effects hcl side tramadol…

tramadol withdrawal symptoms…

Awesome. Your site seems fantastic, that i’m glad i’ve found one thing below worthy of preparing my personal favorites.

Your website was tweeted by a friend yesterday evening. Believed Id check it out. Very best decision ever.

Hi, buzz me if you want to discuss this further!

Post a comment