Compiling is, in a nutshell, source code written in a human-friendly way. It is the work of translating into a machine language that the machine can understand.
The Java compilation mechanism is unique, the javac command is the source code Compile to something called Java bytecode (intermediate code).
The reason why it is not converted to machine language is This is to realize Java's basic philosophy "Write once, run anywhere." If you convert the source code directly to machine language, the compiled machine language will be It will become "dependent" on the environment in which it was compiled, Because the JVM implements the ability to convert Java bytecode to machine language As long as you install the JVM, you will be able to run Java in any environment. In other words, "Once you write the source code, you can execute it even in different environments."
Compiling when javac is executed works as follows.
The important thing here is "1. Type search". When compiling, Java needs information of all types required by the specified class. The specific types you need are:
--Classes and interfaces used in the source code --Classes and interfaces implicitly inherited and implemented in your class
Note the classes that are implicitly inherited and implemented.
For example, to compile the java.util.ArrayList class, ArrayList inherits
It also requires interface information such as Serializable, Iterable
There are several areas in the type search target, and the order in which they are searched is fixed. The areas to be searched are as follows. (Arranged in the order in which they are searched)
--Bootstrap class
A group of classes that includes the core API of the JDK (eg java.lang. *).
It is generally located in <JAVA_HOME> / lib
.
--Extension class
A group of classes placed in the extension directory.
Generally, it is placed in <JAVA_HOME> / lib / ext
.
The above search order is The same is true for class loaders that load class information into memory at run time. For more information on class loaders, see What is a class loader.
When you run javac, the search will be done in the above order, If the same type is detected more than once, the information in the area with the highest search priority will be used.
At compile time, the files found by type search depend on the situation It can be a class file, a source file, or both. The processing performed by javac is divided into the following three depending on the type and status of the found file.
[If only class files are found] Use the class file as it is without compiling
[If only the source file is found] Compile the source file and use the generated class file
[If both are found] ~ If the class file is older than the source file ~ Compile the source file and use the latest generated class file ~ Other than the above ~ Use the class file as it is without compiling
When developing, I'm using the Java2 SDK, so javac is the Java2 SDK Compile for bootstrap classes, but with JDK 1.4 instead of Java 2 SDK You may also want to use the bootstrap class.
In these cases, the -target 1.4
option allows for 1.4 VM compatible class files.
And use -bootclasspath
and -extdirs
to get the JDK 1.4 bootstrap class
Specifies to compile against.
In this way, the function to compile across versions etc. It's called ** cross-compilation **.
Now that you understand all the terms about Java compilation Let's take a look at the options for making various settings and specifications.
--- encoding
--- target
Generate a class file to run on a VM with JDK 1.1 or later.
- 1.2
Generate class files that do not work on VMs prior to JDK 1.2.
- 1.3
Generate class files that do not work on VMs prior to JDK 1.3.
- 1.4
Generate class files that do not work on VMs prior to JDK 1.4.
- 1.5
Generate class files that are compatible only with JDK 5 VMs.
- 5
Synonymous with 1.5.
--- bootclasspath
The following information is required to run UseCommons.java.
--Class with main method (UseCommons.java) --Class information used in the execution class (StrFactory.java, commons-lang3-3.10.jar)
To collect the above information, the javac command needs the following information:
--Location of UseCommons, the class to be executed --Classpath to StrFactory and commons-lang3-3.10.jar used in execution class --Package root of the package where StrFactory is located --commons-lang3-3.10. Jar placement location
The specific commands are as follows.
#The current directory is/java-sample/src
$ javac -cp /java-sample/lib/commons-lang3-3.10.jar:/java-sample/src com/example/app/UseCommons.java
The following is the classpath specification.
#Specify the classpath of the JAR file(Specify up to the file)
/java-sample/lib/commons-lang3-3.10.jar
#Specify the class path of the class file(Do not specify the file)
/java-sample/src
#As mentioned above, when specifying multiple classpaths:(For Windows;)Connect with
-cp /java-sample/lib/commons-lang3-3.10.jar:/java-sample/src
Specifies the last class to execute.
#In the example, it is specified by the relative path from the current directory.
com/example/app/UseCommons.java
Below are the execution results.
If you look, you will find a xxx.class file with the same name as xxx.java.
Also, for the classes defined in UseCommons, UseCommons $ CommonsHelper.class
A file of the format class name $ inner class name.class
is created.
You can compile multiple files using regular expressions. In this case, the classpath to the class required by the target class All must be set.
#Compile all classes in multiple directories
javac multiple/*.java
#Compile all classes called ~ Target in multiple directories
javac multiple/*Target.java
In the examples so far, the class file is created in the same directory, so It takes extra effort to create the JAR file, and the folders are not organized and difficult to see. In such a case, use the -d option to store the class file, and -sourcepath to store the source file. You can specify the storage destination.
#When executed, directly under src.java file is directly under classes.The class file is stored
javac -sourcepath src -d classes /src/com/example/app/Sample.java
The following example assumes a platform with the Java2 SDK set as a prerequisite. When you execute the following command, it is not the Java2 platform API of the Java2 SDK, but It will now compile using the core API of JDK 1.4.
Supplement: About the setting value of -boot class path -bootclasspath contains directories, JAR files, ZIP files, etc. Because it can be specified, a JAR file that stores the bootstrap classes together is stored independently. Creating it saves you the trouble of specifying multiple entries.
# -target 1.4:JDK1.Generate class files compatible with 4 and above
# -bootclasspath <path>:指定したpathにあるブートストラップクラスを使用してコンパイルを行う
javac -target 1.4 -bootclasspath <Bootstrap class archive location> Sample.java
Supplement: How to check the storage location of the bootstrap class The bootstrap class is stored in Set in the system property sun.boot.class.path.
Use a file called the command line argument file for javac There is a mechanism to specify multiple classes and multiple options, but make full use of this Considering the cost, I think it is better to study the build tool, so I will omit it.
Recommended Posts