Using JDK for Compilation
Overview
A Java Development Kit (JDK) is a software package required for Java development. It contains the Java Runtime Environment (JRE) and compilation and commissioning tools. On the basis of OpenJDK, openEuler optimizes GC, enhances concurrency stability, and enhances security, improving the performance and stability of Java applications on ARM.
Basics
File Type and Tool
For any given input file, the file type determines which tool to use for processing. The common file types and tools are described in Table 1 and Table 2.
Table 1 Common JDK file types
Table 2 Common JDK tools
Java running tool, which is used to run .class bytecode files or .jar files. | |
Java Program Generation Process
To generate a program from Java source code files and run the program using Java, compilation and run are required.
- Compilation: Use the Java compiler (javac) to compile Java source code files (.java files) into .class bytecode files.
- Run: Execute the bytecode files on the Java virtual machine (JVM).
Common JDK Options
Javac Compilation Options
The command format for javac compilation is as follows: javac [options] [sourcefiles] [classes] [@argfiles]
In the preceding information:
options: command options.
sourcefiles: one or more source files to be compiled.
classes: one or more classes to be processed as comments.
@argfiles: one or more files that list options and source files. The -J option is not allowed in these files.
Javac is a Java compiler. It has many options, but most of them are not commonly used. Table 3 describes the common options values.
Table 3 Common javac options
Java Running Options
The Java running format is as follows:
Running class file: java [options] classesname [args]
Running Java file: java [options] -jar filename [args]
In the preceding information:
options: command options, which are separated by spaces.
classname: name of the running .class file.
filename: name of the running .jar file.
args: parameters transferred to the main() function. The parameters are separated by spaces.
Java is a tool for running Java applications. It has many options, but most of them are not commonly used. Table 4 describes the common options.
Table 4 Common Java running options
JAR Options
The JAR command format is as follows: jar {c | t | x | u}[vfm0M] [jarfile] [manifest] [-C dir] file...
Table 5 describes the parameters in the jar command.
Table 5 JAR parameter description
Class Library
The Java class library is implemented as a package, which is a collection of classes and interfaces. The Java compiler generates a bytecode file for each class, and the file name is the same as the class name. Therefore, conflicts may occur between classes with the same name. In the Java language, a group of classes and interfaces are encapsulated in a package. Class namespaces can be effectively managed by package. Classes in different packages do not conflict even if they have the same name. This solves the problem of conflicts between classes with the same name and facilitates the management of a large number of classes and interfaces. It also ensures the security of classes and interfaces.
In addition to many packages provided by Java, developers can customize packages by collecting compiled classes and interfaces into a package for future use.
Before using a custom package, you need to declare the package.
Package Declaration
The declaration format of a package is package pkg1[.pkg2[.pkg3...]].
To declare a package, you must create a directory. The subdirectory name must be the same as the package name. Then declare the package at the beginning of the class file that needs to be placed in the package, indicating that all classes of the file belong to the package. The dot (.) in the package declaration indicates the directory hierarchy. If the source program file does not contain the package statement, the package is specified as an anonymous package. An anonymous package does not have a path. Generally, Java still stores the classes in the source file in the current working directory (that is, the directory where the Java source files are stored).
The package declaration statement must be added to the beginning of the source program file and cannot be preceded by comments or spaces. If you use the same package declaration statement in different source program files, you can include the classes in different source program files in the same package.
Package Reference
In Java, there are two methods to use the common classes in the package provided by Java or the classes in the custom package.
Add the package name before the name of the class to be referenced.
For example, name.A obj=new name.A ()
name indicates the package name, A indicates the class name, and obj indicates the object. This string indicates that class A in the name package is used to define an object obj in the program.
Example: Create a test object of the Test class in the example package.
example.Test test = new example.Test();
Use import at the beginning of the file to import the classes in the package.
The format of the import statement is import pkg1[.pkg2[.pkg3...]].(classname | *).
pkg1[.pkg2[.pkg3...]] indicates the package level, and classname indicates the class to be imported. If you want to import multiple classes from a package, you can use the wildcard (*) instead.
Example: Import the Test class in the example package.
import example.Test;
Example: Import the entire example package.
import example.*;
Examples
Compiling a Java Program Without a Package
Run the cd command to go to the code directory. The ~/code directory is used as an example. The command is as follows:
cd ~/code
Compile the Hello World program and save it as HelloWorld.java. The following uses the Hello World program as an example. The command is as follows:
vi HelloWorld.java
Code example:
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } }
Run the following command to compile the code in the code directory:
javac HelloWorld.java
If no error is reported, the execution is successful.
After the compilation is complete, the HelloWorld.class file is generated. You can run the java command to view the result. The following is an example:
$ java HelloWorld Hello World
Compiling a Java Program with a Package
Run the cd command to go to the code directory. The ~/code directory is used as an example. Create the ~/code/Test/my/example, ~/code/Hello/world/developers, and ~/code/Hi/openos/openeuler subdirectories in the directory to store source files.
cd ~/code mkdir -p Test/my/example mkdir -p Hello/world/developers mkdir -p Hi/openos/openeuler
Run the cd command to go to the ~/code/Test/my/example directory and create Test.java.
cd ~/code/Test/my/example vi Test.java
The following is an example of the Test.java code:
package my.example; import world.developers.Hello; import openos.openeuler.Hi; public class Test { public static void main(String[] args) { Hello me = new Hello(); me.hello(); Hi you = new Hi(); you.hi(); } }
Run the cd command to go to the ~/code/Hello/world/developers directory and create Hello.java.
cd ~/code/Hello/world/developers vi Hello.java
The following is an example of the Hello.java code:
package world.developers; public class Hello { public void hello(){ System.out.println("Hello, openEuler."); } }
Run the cd command to go to the ~/code/Hi/openos/openeuler directory and create Hi.java.
cd ~/code/Hi/openos/openeuler vi Hi.java
The following is an example of the Hi.java code:
package openos.openeuler; public class Hi { public void hi(){ System.out.println("Hi, the global developers."); } }
Run the cd command to go to the ~/code directory and use javac to compile the source file.
cd ~/code javac -classpath Hello:Hi Test/my/example/Test.java
After the command is executed, the Test.class, Hello.class, and Hi.class files are generated in the ~/code/Test/my/example, ~/code/Hello/world/developers, and ~/code/Hi/openos/openeuler directories.
Run the cd command to go to the ~/code directory and run the Test program using Java.
cd ~/code java -classpath Test:Hello:Hi my/example/Test
The command output is as follows:
Hello, openEuler. Hi, the global developers.