javac - Unix, Linux Command

There are two ways to pass source code file names to
javac: 

Inner class definitions produce additional class files.
These class files have names combining the inner and outer class names,
such as
MyClass$MyInnerClass.class. 

You should arrange source files in a directory tree that reflects
their package tree.
For example, if you keep all your source files in
/workspace, the source code for
com.mysoft.mypack.MyClass should be in
/workspace/com/mysoft/mypack/MyClass.java. 

By default, the compiler puts each class file in the same
directory as its source file.
You can specify a separate
destination directory with
-d (see
OPTIONS, below).


For example, when you subclass
java.applet.Applet, you are also
using Applet’s ancestor classes:
java.awt.Panel, java.awt.Container, java.awt.Component, and
java.awt.Object. 

When the compiler needs type information, it looks for a source
file or class file which defines the type.
The compiler searches
first in the bootstrap and extension classes, then in the user
class path (which by default is the current directory).
The user class path is defined by setting the
CLASSPATH environment variable or by using the
-classpath command line option.
(For details, see
Setting the Class Path.) 

If you use the
-sourcepath option, the compiler
searches the indicated path for source files; otherwise the
compiler searches the user class path both for class files and
source files.
You can specify different bootstrap or extension
classes with the
-bootclasspath and
-extdirs options; see
Cross-Compilation Options below.


A successful type search may produce a class file, a source file,
or both.
Here is how
javac handles each situation:


By default,
javac considers a class file out of date only if
it is older than the source file.


If the
-sourcepath option is not specified, the user class
path is searched for both source files and class files.


If
-d is not specified,
javac puts the class file in the same
directory as the source file.


Note: The directory specified by
-d is not automatically
added to your user class path.


Note: Classes found through the classpath are subject to
automatic recompilation if their sources are found.



switch (x) {
case 1:
           System.out.println("1");
                //  No  break;  statement here.
case 2:
           System.out.println("2");
}


An argument file can include javac options and source filenames in any
combination.  The arguments within a file can be space-separated or
newline-separated.  Filenames within an argument file are relative to
the current directory, not the location of the argument file.
Wildcards (*) are not allowed in these lists (such as for specifying
*.java). Use of the
@ character to recursively
interpret files is not supported. 


When executing javac, pass in the path and name of each argument
file with the
@ leading character.
When javac encounters an argument beginning with
the character
@, it expands the contents of
that file into the argument list.



  C:> javac @argfile


This argument file could contain the contents of both files shown
in the next example.


Create a file named
options containing:



-d classes
-g
-sourcepath \java\pubs\ws\1.3\src\share\classes



MyClass1.java
MyClass2.java
MyClass3.java



% javac @options @classes



% javac @path1/options @path2/classes



   % ls
      greetings/
   % ls greetings
      Hello.java
   % cat greetings/Hello.java
      package greetings;
  
      public class Hello {
           public static void main(String[] args) {
              for (int i=0; i < args.length; i++) {
                  System.out.println("Hello " + args[i]);
              }
           }
      }
   % javac greetings/Hello.java
   % ls greetings
      Hello.class   Hello.java
   % java greetings.Hello World Universe Everyone
      Hello World
      Hello Universe
      Hello Everyone



   % ls
      greetings/
   % ls greetings
      Aloha.java     GutenTag.java     Hello.java      Hi.java
   % javac greetings/*.java
   % ls greetings
      Aloha.class    GutenTag.class    Hello.class     Hi.class
      Aloha.java     GutenTag.java     Hello.java      Hi.java



   % pwd
      /examples
   % javac greetings/Hi.java


Since
greetings.Hi refers to other classes in the greetings
package, the compiler needs to find these other classes.
The example above works, because our default user class path happens
to be the directory containing the package directory.
But suppose
we want to recompile this file and not worry about which directory
we’re in?
Then we need to add
/examples to the user class path.
We can do this by setting CLASSPATH, but here we’ll use the
-classpath option.



   % javac -classpath \examples /examples/greetings/Hi.java


If we change
greetings.Hi again, to use a banner utility, that
utility also needs to be accessible through the user class path.



   % javac -classpath /examples:/lib/Banners.jar \
                     /examples/greetings/Hi.java


To execute a class in greetings, we need access both to greetings
and to the classes it uses.



   % java -classpath /examples:/lib/Banners.jar greetings.Hi



   % ls
      classes/  lib/      src/
   % ls src
      farewells/
   % ls src/farewells
      Base.java      GoodBye.java
   % ls lib
      Banners.jar
   % ls classes
   % javac -sourcepath src -classpath classes:lib/Banners.jar \
      src/farewells/GoodBye.java -d classes
   % ls classes
      farewells/
   % ls classes/farewells
      Base.class      GoodBye.class


Note: The compiler compiled src/farewells/Base.java, even
though we didn’t specify it on the command line.
To trace automatic compiles, use the
-verbose option.



   % javac -target 1.4 -bootclasspath jdk1.4.2/lib/classes.zip \
        -extdirs "" OldCode.java


The
-target 1.4 option ensures that the generated class files will
be compatible with 1.4
VMs. BY default,
javac compiles for 1.5.


The Java 2 SDk’s
javac would also by default compile against its own
bootstrap classes, so we need to tell
javac to compile against
JDK 1.4 bootstrap classes instead.
We do this with
-bootclasspath and
-extdirs. Failing to do this might allow compilation against a
Java 2 Platform
API that would not be present on a 1.4
VM and would fail at runtime.


jar (1)
jar (1)
java (1)
java (1)
javadoc (1)
javadoc (1)
javah (1)
javah (1)
javap (1)
javap (1)
jdb (1)
jdb (1)



Advertisements


    129 Lectures 
     23 hours 

 Eduonix Learning Solutions

    5 Lectures 
     4.5 hours 

 Frahaan Hussain

    35 Lectures 
     2 hours 

 Pradeep D

    41 Lectures 
     2.5 hours 

 Musab Zayadneh

    46 Lectures 
     4 hours 

 GUHARAJANM

    6 Lectures 
     4 hours 

 Uplatz
 Print
 Add Notes
 Bookmark this page