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.
CD \MyDir javac.exe -classpath . HelloWorld.java
CD \ javac.exe -classpath . com\mindprod\mypackage\HelloWorld.java
| Javac.exe Command Line Switches | |||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Option | Effect | ||||||||||||||||||||||||
| -g | generate all debugging information | ||||||||||||||||||||||||
| -g:none | remove all debugging information | ||||||||||||||||||||||||
| -o | optimise | ||||||||||||||||||||||||
| -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. | ||||||||||||||||||||||||
| -source 1.3 | What source code features you plan to use:
| ||||||||||||||||||||||||
| -target 1.1 | Specifies which the lowest number of JRE you intend to run this code on. You
can also specify -target 1.2 and 1.3. It is not
smart enough to warn you if you use classes or methods not part of that class
library. You have to discover those errors by runtime tests. In Eclipse and
other advanced IDEs you can configure the JDK 1.5 compiler to use the JDK 1.4
library if you have that JRE also installed, which will catch these errors. If
you use -target 1.4 and the 1.5 compiler you can force
the compiler to use the 1.4 classfiles on the command line with
-bootclasspath J:\j2sdk1.4.2_17\jre\lib\rt.jar
That way you can detect use of 1.5 classes that did not exist in 1.4, at compile
time, e.g. StringBuilder.
In JDK 1.3-, the colours had lower case names such as Color.
white. Since these are static
final constants, in JDK 1.4+, Sun gave them proper upper case names such
as Color.WHITE.
However, beware of using the upper case names it you want to target JDK 1.3-
versions. Your programs will explode in a glory of exceptions from the missing
support. The lower case names will work in any JDK version. | ||||||||||||||||||||||||
| -version | Verify you are using the version of javac.exe you think you are using. | ||||||||||||||||||||||||
| -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. Use 4NT to independently redirect stdout and stderr. | ||||||||||||||||||||||||
| -help | Get a list of options and what they do. Trust what it says over what I say here. Knowledge keeps no better than fish. | ||||||||||||||||||||||||
What I just said is not quite true. Javac.exe will look for *.class files on the classpath and if it can’t find them search the sourcepath (-sourcepath option) for the matching *.java files to recompile. By default, the sourcepath is the same as the classpath.
In ant, by default, the sourcepath is null in order to suppress recompiling classes outside the package being compiled. This avoids problems with recompiling with the wrong source or target JDK version. Further ant only recompiles out of date files mentioned on the command line.
When you are testing, sometimes recompiling everything twice is needed to propagate the new versions to all corners, especially when you have jars or circular dependencies between packages i.e. A uses classes of B and B uses classes of A.
Here are cases where javac.exe fails to recompile.
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.
![]() |
and suggestions to improve this page to Roedy Green : | ||
| Canadian Mind Products | |||
| mindprod.com IP:[65.110.21.43] | |||
| Your face IP:[38.103.63.17] | ![]() | ||
| You are visitor number 104,730. | |||
| You can get a fresh copy of this page from: | or possibly from your local J: drive (Java virtual drive/Mindprod website mirror) | ||
| http://mindprod.com/jgloss/javacexe.html | J:\mindprod\jgloss\javacexe.html | ||