Introduction to javac command

Chapter 3 Compiling Source Code

What is compilation in Java?

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."

Overview of the javac command

Compiling when javac is executed works as follows.

  1. Parse the specified class and search for dependent types
  2. Compile the corresponding type in the search

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 , Collection , The information does not appear in the source code.

Search order

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.

Behavior for search results

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

Cross-compile

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 **.

javac command options

Now that you understand all the terms about Java compilation Let's take a look at the options for making various settings and specifications.

Major options

--- encoding Specify the character code of the source file. By default Java platform converter (source code ↔︎ Java bytecode conversion) Use the encoding set to. --- sourcepath Specify the location of the source file. By default, the user classpath is specified. --- d Specify the location of the class file. By default, the user classpath is specified. --- classpath (or -cp) Specify the search destination of the user class. When multiple selections are made, separate them with a semicolon (Windows) or a colon (Unix). --- source Only allow specific JDK versions of the source code.

Debug options

Cross-compilation options

--- target Generates a class file that runs on the specified version of the VM. By default, it will generate class files that are compatible with the JDK5 VM. However, the default is 1.4 only if the options below -source 1.4 are used. The supported versions of javac are: - 1.1
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 Cross-compile for the specified boot class. Like the user classpath Separate multiple entries in the boot classpath with a semicolon or colon. The boot classpath can be a directory, JAR archive, or ZIP archive.

Run

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. スクリーンショット 2020-05-25 21.23.45.png

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.

Other examples of using the javac command

Compiling multiple files

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

Separate storage destinations for source files and class files

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

Cross-compile

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.

Command line argument file

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.

Return to main page

Recommended Posts

Introduction to javac command
Introduction to javadoc command
Introduction to jar command
Introduction to Ruby 2
Introduction to SWING
Introduction to Micronaut 1 ~ Introduction ~
Introduction to migration
Introduction to java
Introduction to Doma
Introduction to JAR files
Introduction to Ratpack (8)-Session
Introduction to RSpec 1. Test, RSpec
Introduction to bit operation
Introduction to Ratpack (6) --Promise
Introduction to Ratpack (9) --Thymeleaf
Introduction to PlayFramework 2.7 ① Overview
Introduction to Android Layout
Introduction to design patterns (introduction)
Introduction to Ratpack (2)-Architecture
Introduction to lambda expression
Introduction to RSpec 2. RSpec setup
Introduction to Keycloak development
Introduction to Design Patterns (Builder)
Introduction to RSpec 5. Controller specs
Introduction to RSpec 6. System specifications
Introduction to RSpec 3. Model specs
Introduction to Ratpack (5) --Json & Registry
Introduction to Metabase ~ Environment Construction ~
Introduction to Ratpack (7) --Guice & Spring
(Dot installation) Introduction to Java8_Impression
Introduction to Design Patterns (Composite)
Introduction to Micronaut 2 ~ Unit test ~
Introduction to JUnit (study memo)
Introduction to Spring Boot ① ~ DI ~
Introduction to design patterns (Flyweight)
[Java] Introduction to lambda expressions
Introduction to Spring Boot ② ~ AOP ~
Introduction to Apache Beam (2) ~ ParDo ~
[Ruby] Introduction to Ruby Error statement
Introduction to EHRbase 2-REST API
Introduction to design patterns Prototype
GitHub Actions Introduction to self-made actions
[Java] Introduction to Stream API
Introduction to Design Patterns (Iterator)
Introduction to Spring Boot Part 1
Introduction to Ratpack (1) --What is Ratpack?
XVim2 introduction memo to Xcode12.3
Introduction to RSpec-Everyday Rails Summary-
Introduction to Design Patterns (Strategy)
[Introduction to rock-paper-scissors games] Java
Introduction to Linux Container / Docker (Part 1)
Introduction to swift practice output Chapter5
[Introduction to Java] About lambda expressions
Introduction to algorithms in java-cumulative sum
[Introduction to Java] About Stream API
Introduction to Functional Programming (Java, Javascript)
OpenJDK 8 java and javac command help
Introduction to Ruby processing system self-made
Introduction to algorithms with java-Shakutori method
Introduction to Design Patterns (Factory Method)
Introduction to Linux Container / Docker (Part 2)