Java Glossary : javac.exe

CMP home Java glossary home Menu no menu Last updated 2004-06-28 by Roedy Green ©1996-2004 Canadian Mind Products

Java definitions: 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

You are here : home : Java Glossary : J words : javac.exe.

javac.exe
Sun's free Java compiler that comes bundled with the JDK. If you install Java 1.4.2 in the default directory you should find it in J:\j2sdk1.4.2_04\bin\javac.exe. You normally invoke it from the command line, but you can also invoke it from within a Java program. Use the sun.tools.javac.Main class located in %java_home%/lib/tools.jar to pass it an array of Strings equivalent to the command line parameters. You might do this to bulk-compile large numbers of files without reloading the javac compiler for each file.

JDK Tools Documentation Pack : available:

Class and File Naming Rules

A class name must start with a capital letter. The source for that class must live in a plain text file with the same name (including case), with a *.java extension.

You cannot put more than one public class inside a *.java file.

Classes in the same package are accessible to each other, even if they are in different files. To access other classes, you must import them.

The source code must be stored in a directory with the same name as the package declared at the top of the file, including case, with the dots replaced by \ or / or whatever your platform uses for directory element separators.

Package names must be lower case, usually beginning com.yourwebsite to ensure global uniqueness.

It is a good idea to put every class in some package. Only experiments you plan to keep for under an hour should be without a package.

CLASSPATH And File Naming Recipes

Here are my simplified rules for using CLASSPATH and naming the files on the javac.exe command line:
  1. Use JDK 1.1 or later, why not the latest?. Configure your SET CLASSPATH= to clear it out. Avoid JDK 1.0 if you can because its CLASSPATH is more complicated since it has to help javac find the standard classses.
  2. Download SmartJ. It forgives most problems with classpath and file naming, even if you use JDK1.1+. Unfortunately, it has not been updated to work with JDK 1.2 or 1.3.
  3. In all that follows, everything is strictly case sensitive.
  4. To compile a HelloWorld.java app in the default package in C:\MyDir , use

    CD \MyDir
    javac.exe -classpath . HelloWorld.java
    

  5. To compile a HelloWorld.java app in C:\com\mindprod\mypackage , in package com.mindprod.mypackage, use

    CD \
    javac.exe -classpath . com\mindprod\mypackage\HelloWorld.java
    

The Javac.exe Command Line

Javac.exe Command Line Switches
Option Effect
-g generate all debugging information
-o optimise
-debug add information to class files to aid in debugging. It still won't get them all.
-Xdepend Use a much slower more conservative approach to deciding which files need to be recompiled.
-nowarn suppress warning messages.
-verbose long version of error messages.
-classpath /mydir:/place/myCollection.jar overriding CLASSPATH, colon separated. Infuriatingly, javac.exe won't let you use the -cp shortcut.
-nowrite don't actually generate code, just check the syntax.
-deprecation warn of any use of any deprecated methods.
-d targetDir Place the output class files in this directory rather than the usual same directory as source.
-encoding UTF-8 what encoding was used to create the source files. Lets you embed fancy characters you would otherwise need to encode with \uxxxx.
-J runtimeflag  
@listOfFiles.txt To shorten or simplify the javac command, you may specify one or more files that themselves contain one filename per line. This enables you to overcome the command-line length limitation of Windows.
-target 1.1 Specifies which JRE will be used. You can also specify -target 1.2 and 1.3. It is not smart enough to warn you if you use classes not part of that class library.
-Xstdout Send error messages to stdout instead of stderr. This makes them easier to redirect to a file in Windows. For some idiotic reason this is no longer supported under JDK 1.3, (a result of intercorporate sniping at Microsoft?) It is supported with oldjavac.exe .
-help Get a list of options and what they do. Trust what it says over what I say here. Knowledge keeps no better that fish.
Note there is no -cp or -jar option! Instead you must use -classpath for both functions.

Programmatic Invocation

You can also invoke the javac compiler as an undocumented class, e.g.:

sun.tools.javac.Main comp = new sun.tools.javac.Main( System.out, null );
boolean success = comp.compile( new String [] { "HelloWorld.java"} );

You might do this for speed to avoid reloading javac.exe , or to have arbitrarily long command lines. You can also get long command lines with the @listOfFiles technique.

Capturing the error messages from javac.exe can be a hassle since they scroll off the screen faster than you can read them. Ordinary > redirection won't capture them to a file because they are going to STDERR, not STDOUT. I use a program called 4NT to solve this problem. It lets you independently redirect STDOUT and STDERR. You can also use the -Xstdout to bypass the problem.

What Gets Recompiled?

javac.exe will recompile all the *.java files you mention on the command line, whether they need it or not. Further, if your java source references *.java files not mentioned on the command line and they are out of date, it will recompile them too. However, it is still possible to miss recompiling something that needs it. That is why you periodically should recompile the universe especially before any release or global testing. Here are two case where javac.exe fails to recompile.
  1. If you recompile A, and A depends on B (which has not changed) and B depends on C (which has changed), Javac will not recompile C, if you invoke it on A.
  2. If you change the value of a final static value in a class and recompile it, javac won't necessarily recompile any other dependent classes which reference that field, and have the old value burned into the class file as a literal.
Using a make utility to figure out what needs recompiling usually won't do any better. It will take much longer since it will load javac.exe once per source file. You might as well recompile everything with javac.exe *.java. Invoking javac.exe once per source file or even once per directory will really slow you down. It is much better to invoke javac.exe once, and feed it everything either via the command line or the programmatic interface.

My rule of thumb is to erase all class files before a full application test or release. Also erase them if you suspect you are getting an old class file somewhere along the line. Most of the time, selective recompilation works fine. Usually you make changes to only one java file at a time before recompiling, and none of the other files need it. Any time you change public static final constants is a good time to delete all the class files.

Periodically delete all class files. This cleans up class files from renamed, and deleted source files. It gets rid of unused anonymous inner class files. Strange things can happen when you rename a module, and fail to fix all the references, and still leave the old class file lying about even if you delete the source.


CMP logo
CMP_home
home
Canadian Mind Products CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[24.87.56.253]
Your IP:[80.134.30.163]
You are visitor number 12206.
Please send errors, omissions and suggestions
to improve this page to Roedy Green.
You can get a fresh copy of this page from: or possibly from your local J: drive mirror:
http://mindprod.com/jgloss/javacexe.html J:\mindprod\jgloss\javacexe.html