Document directory
- Associated editing options
- Non-standard options
- Compile a simple program
- Compile multiple source files
- Specify user class path
- Separate source files from class files
- Code examples
Structure
javac [ options ] [ sourcefiles ] [ @files ]
Parameters can be arranged in any order.
-
options
-
Command line options.
-
sourcefiles
-
One or more source files to be compiled (for example, myclass. Java ).
-
@files
-
One or more files listing source files.
Description
The javac tool reads classes and interface definitions written in the Java programming language and compiles them into bytecode class files.
There are two ways to pass the source code file nameJavac:
- If the number of source files is small, you can list the file names on the command line.
- If the number of source files is large, the source files are listed in one file. The names are separated by spaces or return lines. ThenJavacUse the name of the list file in the command line.@Character.
The source code file name must contain.java
Suffix. The class file name must contain.class
Suffix. Both the source file and the class file must have the root name that identifies the class. For exampleMyClass
Class will be written inMyClass.java
And is compiled into bytecode files.MyClass.class
.
Internal class definitions generate additional class files. The names of these class files combine the names of internal and external classes. For exampleMyClass$MyInnerClass.class
.
The source files should be arranged in the directory tree that reflects the package tree structure. For example, if you put all the source files in/WorkspaceMedium, thencom.mysoft.mypack.MyClass
The Code should be in/Workspace/com/mysoft/mypack/MyClass. java.
By default, the compiler places each class file in the same directory as its source file. Available-DOption (seeOption) Specify other target directories.
Search type
When compiling a source file, the compiler often needs information about the types it has not recognized. For each class or interface used, extended, or implemented in the source file, the compiler requires its type information. This includes classes and interfaces that are not explicitly mentioned in the source file but provide information through inheritance.
For exampleJava. applet. AppletAlso usedAppletAncestor class:Java. awt. Panel,Java. awt. Container,Java. awt. ComponentAndJava. awt. Object.
When the compiler needs the type information, it searches for the source file or class file of the definition type. The compiler first searches for the UDF class and extension class, and then searches for it in the user class path. User class paths are defined in two ways: by settingCLASSPATHEnvironment variable or usage-ClasspathCommand line options. (For more information, seeSet the class path). If you use-SourcepathInSourcepathFind the source file in the specified path; otherwise, the compiler searches for the class file and source file in the user class path. Available-BootclasspathAnd-ExtdirsTo specify different auto-initiated or extended classes. For more information, seeAssociated editing options.
A successful type search may generate class files, source files, or both. Below isJavacHandling of various situations:
Note:JavacYou can implicitly compile some source files not mentioned in the command line. Use-VerboseOption to track automatic compilation.
File List
To shorten or simplifyjavac
Command to specify one or more files with one file name in each line. In the command line, use'@'Specifies a File List by adding a file name to the string. When javac encounters'@'When a parameter starts with a character, it performs the same operation on the file name contained in that file as on the file name in the command line. This makes Windows command line length no longer limited.
For examplesourcefiles
To list the names of all source files. This file may be like:
MyClass1.java MyClass2.java MyClass3.java
Run the compiler with the following command:
C:> javac @sourcefiles
Option
The compiler has a number of standard options. The current development environment supports these standard options and will support them in future versions. There are also a number of additional non-standard options that are unique to the current Virtual Machine implementation and may change in the future. Non-standard options-XHeaders.
Standard options
-
-Classpath
Class path
-
Sets the user class path, which will overwrite
ClasspathPath of the user class in the environment variable. If no
ClasspathNot Specified
-ClasspathThe user class path consists of the current directory. For more information, see
Set the class path.
If not specified-SourcepathTo find the class files and source files in the user class path.
-
-D
Directory
-
Set the target directory of the class file. If a class is a part of a package
JavacPut this type of file into a subdirectory that reflects the package name, and create a directory if necessary. For example, if you specify
-D c:/myclassesAnd the class name is
com.mypackage.MyClass
, The class file is called
c:/myclasses/com/mypackage/MyClass.class
.
If not specified-DOption, thenJavacPut the class file in the same directory as the source file.
Note:-DThe directory specified by the option will not be automatically added to the user class path.
-
-Deprecation
-
Displays descriptions of the use or overwrite of each type of unencouraged members or classes. Not given
-DeprecationOption,
JavacThe names of these source files will be displayed: These source files use or overwrite unencouraged members or classes.
-
-Encoding
-
Set the source file encoding name, for example
EUCJIS/SJIS
. If not specified
-EncodingThe default platform converter is used.
-
-G
-
Generate all debugging information, including local variables. By default, only the row number and source file information are generated.
-
-G: None
-
No debugging information is generated.
-
-G:
{Keyword list}
-
Only some types of debugging information are generated. These types are specified by the comma-separated keyword list. Valid keywords:
-
Source
-
Source File debugging information
-
Lines
-
Line number debugging information
-
Vars
-
Local variable debugging information
-
-Nowarn
-
Disable warning information.
-
-O
-
Optimize the code to shorten the execution time. Use
-OThis option may reduce Compilation speed, generate larger class files, and make the program difficult to debug.
In versions earlier than JDK 1.2-GOptions and-OOption cannot be used together. In JDK 1.2-GAnd-OOptions are combined, but unexpected results may be obtained, such as losing variables, locating code again, or losing code.-OOption is not automatically Enabled-DependOr close-G. Similarly,-OThis option is no longer allowed for cross-class embedding.
-
-Sourcepath
Source Path
-
Specifies the source code path used to find the class or interface definition. Like the user class path, the Source Path item uses a semicolon (
;Directory, JAR file, or ZIP file. If a package is used, the local path name in the directory or archive file must reflect the package name.
Note: If the source file of a class is found through the class path, it may be automatically re-compiled.
-
-Verbose
-
Lengthy output. It includes information about each loaded class and each compiled source file.
Associated editing options
By default, the class is based onJavacJDK self-raising class and extension class released together for compilation. HoweverJavacAlso supportedJoint editingIn the joint editing, classes are compiled based on the self-lifting and extension classes implemented by other Java platforms. During joint compilation,-BootclasspathAnd-ExtdirsIs very important; see the followingCode examples.
-
-Target
Version
-
Generate a class file that will run on the virtual machine of the specified version. By default, class files that are compatible with virtual machines 1.1 and 1.2 are generated. In JDK 1.2
JavacSupported versions include:
-
1.1
-
Ensure that the generated class files are compatible with virtual machines of Version 1.1 and version 1.2. This is the default status.
-
1.2
-
The generated class file can be run on a VM of version 1.2, but cannot run on a VM of Version 1.1.
-
-Bootclasspath
Custom class path
-
Concatenates a dataset Based on the specified UDF class set. Like the user classless path, use a semicolon (
;Directory, JAR file, or ZIP file.
-
-Extdirs
Directory
-
Concatenates Based on the specified extended directory.
DirectoryA list of directories separated by semicolons. Search for class files in each JAR archive file in the specified directory.
Non-standard options
-
-X
-
Displays information about non-standard options and exits.
-
-Xdepend
-
Recursively search all available classes to find the latest source files to be recompiled. This option will more reliably find the classes to be compiled, but will greatly slow down the compilation process.
-
-Xstdout
-
Send the compiler information
System.out
. By default, the compiler information is sent
System.err
.
-
-Xverbosepath
-
Describes how to search for paths and standard extensions to find source files and class files.
-
-J
Option
-
Set
OptionPass
JavacCalled
JavaInitiator. For example,
J-Xms48mSet the startup memory to 48 MB. Although it does not
-XBut it is not
Javac'. Use
-JIt is a public convention to pass the option to the virtual machine that executes the application program written in Java.
Note:CLASSPATH,-Classpath,-BootclasspathAnd-ExtdirsAndNoUsed for runningJavac. Such misuse of the compiler usually makes no sense and is always dangerous. If this is required-JOption to pass the option to the basicJavaInitiator.
Program example
Compile a simple program
A source fileHello.java
It definesGreetings. Hello.greetings
Directory is the package directory of the source file and the class file, and it is not the current directory. This allows us to use the default user class path. It also makes it unnecessary for us to use-DOption to specify a separate target directory.
C:> dirgreetings/C:> dir greetingsHello.javaC:> cat greetings/Hello.javapackage greetings;public class Hello { public static void main(String[] args) { for (int i=0; i < args.length; i++) { System.out.println("Hello " + args[i]); } }}C:> javac greetings/Hello.javaC:> dir greetingsHello.class Hello.javaC:> java greetings.Hello World Universe EveryoneHello WorldHello UniverseHello Everyone
Compile multiple source files
Compile this examplegreetings
All source files in the package.
C:> dirgreetings/C:> dir greetingsAloha.java GutenTag.java Hello.java Hi.javaC:> javac greetings/*.javaC:> dir greetingsAloha.class GutenTag.class Hello.class Hi.classAloha.java GutenTag.java Hello.java Hi.java
Specify user class path
Modify a source file in the preceding example and recompile it:
C:> cd/examplesC:> javac greetings/Hi.java
Becausegreetings.Hi
Referencedgreetings
For other classes in the package, the compiler needs to find these other classes. The preceding example runs because the Default User class path is the directory containing the package directory. However, if we want to re-compile the file and do not care which directory we are in, we need/examples
Add to user class path. You can setClasspathTo achieve this goal, but here we will use-Classpath.
C:>javac -classpath /examples /examples/greetings/Hi.java
Ifgreetings.Hi
To use the title utility, the utility also needs to be accessed through the user class path:
C:>javac -classpath /examples:/lib/Banners.jar / /examples/greetings/Hi.java
To executegreetings
To accessgreetings
And the class it uses.
C:>java -classpath /examples:/lib/Banners.jar greetings.Hi
Separate source files from class files
It is often meaningful to place source files and class files in different directories, especially in large projects. We use-DOption to specify the target location of a separate class file. Because the source file is not in the user class path-SourcepathTo help the compiler find them.
C:> dirclasses/ lib/ src/C:> dir srcfarewells/C:> dir src/farewellsBase.java GoodBye.javaC:> dir libBanners.jarC:> dir classesC:> javac -sourcepath src -classpath classes:lib/Banners.jar / src/farewells/GoodBye.java -d classesC:> dir classesfarewells/C:> dir classes/farewellsBase.class GoodBye.class
Note: The compiler has compiledsrc/farewells/Base.java
, Although we didn't specify it in the command line. To track automatic compilation, you can use-Verbose.
Code examples
Here we use JDK 1.2JavacTo compile the code that will run on a VM of Version 1.1.
C:> javac -target 1.1 -bootclasspath jdk1.1.7/lib/classes.zip / -extdirs "" OldCode.java
The-target 1.1 option ensures that the generated class files are compatible with the 1.1 virtual machine. In JDK1.2, by defaultJavacThis option is not strictly required because the compiled file is compatible with the 1.1 virtual machine. However, since other compilers may adopt other default settings, it is a good habit to provide this option.
JDK 1.2JavacIn the default status, the SDK will also be compiled based on the 1.2 version of the UDF class. Therefore, we need to tellJavacCompile the SDK Based on the JDK 1.1 custom class. Available-BootclasspathAnd-ExtdirsTo achieve this goal. Otherwise, the compiler may compile Based on the 1.2 API. Because the 1.1 version of the virtual machine may not have the 1.2 version of the API, so the runtime error.
Original article:Http://www.iplab.cs.tsukuba.ac.jp /~ Liuxj/jdk1.2/zh/docs/tooldocs/Win32/javac.html