tencent cloud

TencentOS Server

Java Development Guide

ダウンロード
フォーカスモード
フォントサイズ
最終更新日: 2026-10-19 17:22:51
AI翻訳

Introduction to Java

Java is an object-oriented, high-level programming language. Java applications are typically compiled into bytecode that can run on the Java Virtual Machine (JVM), independent of the underlying computer architecture, thus achieving "write once, run anywhere." Java is currently widely used in client-server Web application development.

Java Component Support Status

TencentOS Server currently integrates three versions of its self-developed JDK (Java runtime and Java virtual machine): TencentKona-17, TencentKona-11, and TencentKona-8.

Installing the JDK

Installing TencentKona-17

The installation command is as follows:
dnf install java-17-konajdk -y
After installation, run the following command to verify:
java -version
The reference output is as follows:
openjdk 17.0.3-ga 2022-04-19
OpenJDK Runtime Environment TencentKonaJDK (build 17.0.3-ga+1)
OpenJDK 64-Bit Server VM TencentKonaJDK (build 17.0.3-ga+1, mixed mode, sharing)

Installing TencentKona-11

The installation command is as follows:
dnf install java-11-konajdk -y
After installation, run the following command to verify:
java -version
The reference output is as follows:
openjdk 11.0.18-ga 2023-01-17
OpenJDK Runtime Environment TencentKonaJDK (build 11.0.18-ga+1)
OpenJDK 64-Bit Server VM TencentKonaJDK (build 11.0.18-ga+1, mixed mode, sharing)

Installing TencentKona-8

The installation command is as follows:
dnf install java-8-konajdk -y
After installation, run the following command to verify:
java -version
The reference output is as follows:
openjdk version "1.8.0_322"
OpenJDK Runtime Environment (Tencent Kona 8.0.13) (build 1.8.0_322-1)
OpenJDK 64-Bit Server VM (Tencent Kona 8.0.13) (build 25.322-b1, mixed mode, sharing)

Version Selection for Multiple JDK Versions

The JDK is installed by default in the /usr/lib/jvm/java-$-konajdk-$/ directory, and /usr/bin/java points to the java file in the installation directory through a symbolic link. When multiple JDK versions exist in the environment, java points to the JDK version that was installed first. You can set the default JDK by running the following command:
alternatives --config java

Uninstalling the JDK

Uninstalling TencentKona-17

The uninstall command is as follows:
dnf remove java-17-konajdk-headless -y

Uninstalling TencentKona-11

The uninstall command is as follows:
dnf remove java-11-konajdk-headless -y

Uninstalling TencentKona-8

The uninstall command is as follows:
dnf remove java-8-konajdk-headless -y

Basic Usage of Java

Running Java Files

The sample code file MyFirstClass.java is as follows:
public class MyFirstClass {

public static void main(String... args) {
System.out.println("Hello, World!");
}
}
A single java source file can be run directly without compilation, as shown below:
java MyFirstClass.java

Running Java Classes

You need to compile the java source code into a java class using javac, and then run it. The steps are as follows:
1. Check the java version.
java -version
2. Install a java compiler that matches the current java version. The installation command is as follows (using TencentKona-17 as an example):
dnf install java-17-konajdk-devel
3. Compile the java class.
javac MyFirstClass.java
4. Run the java class.
java MyFirstClass

Java Module Package Management

Managing Module Packages via rpm

TencentOS Server integrates some basic Java third-party libraries and packages them as RPM packages. The RPM package name is usually the same as the corresponding JAR package name, and the RPM package can be installed directly by using the dnf command, for example:
dnf install apache-commons-cli -y
Here is an example of installing the apache-commons-cli module package.

Managing Module Packages via maven

maven is a software project management tool based on the Project Object Model (POM) concept.

Installing maven

The maven package has been packaged as an rpm package. TencentOS Server currently supports multiple versions of JDK. To use a specific version, you also need to specify the version of maven-openjdkX, as shown below:
dnf install maven maven-openjdk17 -y
After installation, run the following command to verify:
mvn -v
The reference output is as follows:
Apache Maven 3.8.6
Maven home: /usr/share/maven
Java version: 17.0.3-ga, vendor: Tencent Company, runtime: /usr/lib/jvm/java-17-konajdk
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", arch: "x86_64", family: "unix"
If the currently installed maven-openjdk version does not meet your requirements, uninstall the current maven-openjdk version first, and then install another version.

Creating a maven Project

Create a maven project. The command is as follows:
mvn archetype:generate -DgroupId=com.testmaven.myfirstapp -DartifactId=myfirstapp -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false
This command will create a maven project named myfirstapp and automatically download the required plugin packages from the maven central repository. By default, the plugin packages are stored in ~/.m2.
The project directory structure is as follows:
myfirstapp/
├── pom.xml
└── src
├── main
│ └── java
│ └── com
│ └── testmaven
│ └── myfirstapp
│ └── App.java
└── test
└── java
└── com
└── testmaven
└── myfirstapp
└── AppTest.java
pom.xml is the maven project configuration file, which contains basic project information, dependencies, build methods, and more.
src/main/java stores the project source code.
src/main/java stores the project test source code.

pom Configuration

A simple example of pom.xml is as follows:
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.testmaven.myfirstapp</groupId>
<artifactId>myfirstapp</artifactId>
<version>1.0-SNAPSHOT</version>

<name>myfirstapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
...
</plugins>
</pluginManagement>
</build>
</project>

Building a maven Project

Run the following command to build:
mvn package
The reference output is as follows:
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------< com.testmaven.myfirstapp:myfirstapp >-----------------
[INFO] Building myfirstapp 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ myfirstapp ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/myfirstapp/src/main/resources
...
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.testmaven.myfirstapp.AppTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.02 s - in com.testmaven.myfirstapp.AppTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ myfirstapp ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.197 s
[INFO] Finished at: 2023-05-26T11:59:51+08:00
[INFO] ------------------------------------------------------------------------

Java Program Debugging and Testing

You can run the java program using the jdb command to enter debug mode:
jdb MyFirstClass.java
The reference example is as follows:
[root@localhost new]# jdb MyFirstClass.java
Initializing jdb ...
> stop in MyFirstClass.main
Deferring breakpoint MyFirstClass.main.
It will be set after the class is loaded.
> run
run MyFirstClass.java
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
>
VM Started: Set deferred breakpoint MyFirstClass.main

Breakpoint hit: "thread=main", MyFirstClass.main(), line=4 bci=0
4 System.out.println("Hello, World!");

main[1] where
[1] MyFirstClass.main (MyFirstClass.java:4)
[2] jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (native method)
[3] jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:77)
[4] jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
[5] java.lang.reflect.Method.invoke (Method.java:568)
[6] com.sun.tools.javac.launcher.Main.execute (Main.java:419)
[7] com.sun.tools.javac.launcher.Main.run (Main.java:192)
[8] com.sun.tools.javac.launcher.Main.main (Main.java:132)
main[1]
In the example, first use the stop in command to set a breakpoint at the main function, and then run the program with the run command. After the program pauses at the breakpoint, use the where command to print the current call stack. To view more commands, enter the help command in the jdb interactive interface.


ヘルプとサポート

この記事はお役に立ちましたか?

フィードバック