Long-Term Supported Versions

    Innovation Versions

      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

      Extension (Suffix)

      Description

      .java

      Java source code file.

      .class

      Java bytecode file, which is intermediate code irrelevant to any specific machine or OS environment. It is a binary file, which is the target code file generated after the Java source file is compiled by the Java compiler.

      .jar

      JAR package of Java files.

      Table 2 Common JDK tools

      Name

      Description

      java

      Java running tool, which is used to run .class bytecode files or .jar files.

      javac

      Compiles Java source code files into .class bytecode files.

      jar

      Creates and manages 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.

      1. Compilation: Use the Java compiler (javac) to compile Java source code files (.java files) into .class bytecode files.
      2. 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

      options Value

      Description

      Example

      -d path

      Path for storing the generated class files.

      By default, the class files generated after compilation are in the same path as the source file. You can use the -d option to export the class files to the specified path.

      # Use the -d option to export all class files to the bin directory.

      javac /src/*.java -d /bin

      -s path

      Path for storing the generated source files.

      -

      -cp path or -classpath path

      Searches for the class files required for compilation and specifies the location of the class files.

      # In the Demo, the getLine() method in the GetStringDemo class needs to be invoked. The .class file compiled by the GetStringDemo class is stored in the bin directory.

      javac -cp bin Demo.java -d bin

      -verbose

      Outputs information about the operations being performed by the compiler, such as loaded class information and compiled source file information.

      # Display information about the operations that are being performed by the compiler.

      javac -verbose -cp bin Demo.java

      -source sourceversion

      Specifies the location of the input source files to be searched for.

      -

      -sourcepath path

      Searches for source files (Java files) required for compilation and specifies the location of the source files to be searched for, for example, JAR, ZIP, or other directories that contain Java files.

      -

      -target targetversion

      Generates class files of a specific JVM version. The value can be 1.1, 1.2, 1.3, 1.4, 1.5 (or 5), 1.6 (or 6), 1.7 (or 7), or 1.8 (or 8). The default value of targetversion is related to sourceversion of the -source option. The options of sourceversion are as follows:

      • 1.2, corresponding to target version 1.4
      • 1.3, corresponding to target version 1.4
      • 1.5, 1.6, 1.7, and unspecified, corresponding to target version 1.8
      • For other values, the values of targetversion and sourceversion are the same.

      -

      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

      options Value

      Description

      Example

      -cp path or -classpath path

      Specifies the location of the file to be run and the class path to be used, including the .jar, .zip, and class file directories.

      If there are multiple paths, separate them with colons (:).

      -

      -verbose

      Outputs information about the operations being performed by the compiler, such as loaded class information and compiled source file information.

      # Display information about the operations that are being performed by the compiler.

      java -verbose -cp bin Demo.java

      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

      Parameter

      Description

      Example

      c

      Creates a JAR package.

      # Compress the hello.class files in the current directory into Hello.jar. The compression process is not displayed. If the Hello.jar files do not exist, create them. Otherwise, clear the directory.

      jar cf Hello.jar hello.class

      t

      Lists the contents of a JAR package.

      # List the files contained in Hello.jar.

      jar tf Hello.jar

      x

      Decompresses a JAR package.

      # Decompress Hello.jar to the current directory. No information is displayed.

      jar xf Hello.jar

      u

      Updates the existing JAR package, for example, add files to the JAR package.

      -

      v

      Generates a detailed report and prints it to the standard output.

      # Compress the hello.class files in the current directory into Hello.jar and display the compression process. If the Hello.jar files do not exist, create them. Otherwise, clear the directory.

      jar cvf Hello.jar hello.class

      f

      Specifies the name of a JAR package. This parameter is mandatory.

      -

      m

      Specifies the manifest file to be contained.

      -

      0

      If this parameter is not set, the generated JAR package is larger but faster than that generated when this parameter is not set.

      -

      M

      If the manifest file of all items is not generated, this parameter will be ignored.

      # Compress the hello.class files in the current directory into Hello.jar and display the compression process. If the Hello.jar files do not exist, create them. Otherwise, clear the directory. However, the manifest file is not generated when Hello.jar is created.

      jar cvfM Hello.jar hello.class

      jarfile

      JAR package, which is an auxiliary parameter of the f parameter.

      -

      manifest

      Manifest file in .mf format, which is an auxiliary parameter of the m parameter.

      -

      -C dir

      Runs the jar command in the specified dir. This command can be used only with parameters c and t.

      -

      file

      Specifies the file or path list. All files in the file or path (including those in the recursive path) are compressed into the JAR package or the JAR package is decompressed to the path.

      # Compress all class files in the current directory into Hello.jar and display the compression process. If the Hello.jar files do not exist, create them. Otherwise, clear the directory.

      jar cvf Hello.jar *.class

      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

      1. 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 
        
      2. 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");     
              } 
        }
        
      3. Run the following command to compile the code in the code directory:

        $ javac HelloWorld.java 
        

        If no error is reported, the execution is successful.

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

      1. 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
        
      2. 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();
          }
        }
        
      3. 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.");
          }
        }
        
      4. 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.");
          }
        }
        
      5. 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.

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

      Bug Catching

      Buggy Content

      Bug Description

      Submit As Issue

      It's a little complicated....

      I'd like to ask someone.

      PR

      Just a small problem.

      I can fix it online!

      Bug Type
      Specifications and Common Mistakes

      ● Misspellings or punctuation mistakes;

      ● Incorrect links, empty cells, or wrong formats;

      ● Chinese characters in English context;

      ● Minor inconsistencies between the UI and descriptions;

      ● Low writing fluency that does not affect understanding;

      ● Incorrect version numbers, including software package names and version numbers on the UI.

      Usability

      ● Incorrect or missing key steps;

      ● Missing prerequisites or precautions;

      ● Ambiguous figures, tables, or texts;

      ● Unclear logic, such as missing classifications, items, and steps.

      Correctness

      ● Technical principles, function descriptions, or specifications inconsistent with those of the software;

      ● Incorrect schematic or architecture diagrams;

      ● Incorrect commands or command parameters;

      ● Incorrect code;

      ● Commands inconsistent with the functions;

      ● Wrong screenshots.

      Risk Warnings

      ● Lack of risk warnings for operations that may damage the system or important data.

      Content Compliance

      ● Contents that may violate applicable laws and regulations or geo-cultural context-sensitive words and expressions;

      ● Copyright infringement.

      How satisfied are you with this document

      Not satisfied at all
      Very satisfied
      Submit
      Click to create an issue. An issue template will be automatically generated based on your feedback.
      Bug Catching
      编组 3备份