SoFunction
Updated on 2025-04-07

Java language installation, configuration, compilation and operation process

introduction

As one of the most widely used programming languages ​​in the world, Java has powerful cross-platform features and rich ecosystems, making it an important position in enterprise-level applications, mobile development, big data processing and other fields.

Correctly configuring the Java development environment is a must-have skill for every Java developer.

This article will introduce in detail how to install and configure the Java environment on different operating systems, and show how to compile and run Java programs.

1. Installation of Java environment

1.1 Install JDK

The Java Development Toolkit (JDK) is the basis for Java development. JDK includes tools such as compiler (javac), runtime environment (JRE), debugger (jdb).

1.1.1 Install JDK on Windows

Download JDK:accessOracle Official WebsiteorOpenJDKDownload the JDK installation package for Windows.

Run the installer: Double-click the downloaded installation package and follow the prompts to install it. The recommended installation path isC:\Program Files\Java\jdk-<version>

Configure environment variables

  • Right-click "This Computer" and select "Properties" -> "Advanced System Settings" -> "Environment Variables".
  • Found in System VariablesPath, click "Edit" to add JDKbindirectory path, e.g.C:\Program Files\Java\jdk-<version>\bin
  • NewJAVA_HOMEvariable, installation path with value JDK, for exampleC:\Program Files\Java\jdk-<version>

1.1.2 Install JDK on macOS

Download JDK: Same visitOracle Official WebsiteorOpenJDKDownload the JDK installation package for macOS.

Run the installer: Double-click to download.dmgFile, follow the prompts to install JDK.

Configure environment variables

  • Open the terminal and enternano ~/.bash_profileornano ~/.zshrc(depending on the type of shell used).

Add the following line:

export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=$JAVA_HOME/bin:$PATH
  • Save and exit, and then executesource ~/.bash_profileorsource ~/.zshrcMake the configuration take effect.

1.1.3 Installing JDK on Linux

Download JDK:accessOracle Official WebsiteorOpenJDKDownload the JDK installation package for Linux, or use the package manager to install it.

Install using package manager

For Debian-based systems (such as Ubuntu), do:

sudo apt update
sudo apt install openjdk-<version>-jdk

For Red Hat-based systems (such as CentOS), do:

sudo yum install java-<version>-openjdk-devel

Configure environment variables

  • Open the terminal and enternano ~/.bashrcornano ~/.zshrc

Add the following line:

export JAVA_HOME=/usr/lib/jvm/java-<version>-openjdk
export PATH=$JAVA_HOME/bin:$PATH
  • Save and exit, and then executesource ~/.bashrcorsource ~/.zshrcMake the configuration take effect.

2. Verify Java installation

After the installation is complete, verify that the Java installation is successful and that the environment variables are configured correctly.

Verify JDK installation

  • Open the command line tool (Command Prompt on Windows, Terminals on macOS and Linux).
  • enterjava -versionandjavac -version, the installed Java version information should be displayed.
$ java -version
java version "17.0.1" 2021-10-19 LTS
Java(TM) SE Runtime Environment (build 17.0.1+12-LTS-39)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.1+12-LTS-39, mixed mode, sharing)

$ javac -version
javac 17.0.1

3. Write, compile and run Java programs

3.1 Writing Java Programs

Write Java code using any text editor (such as Notepad++, Sublime Text, VSCode) or integrated development environment (IDE, such as IntelliJ IDEA, Eclipse, NetBeans).

Sample code

public class HelloWorld {
    public static void main(String[] args) {
        ("Hello, World!");
    }
}

3.2 Compiling Java programs

Compile using command line tools:

  1. Open the command line tool.
  2. Navigate toThe directory where the file resides.
  3. Execute the following command to compile:
javac 

After the compilation is successful, aFile, which is a bytecode file that a Java virtual machine (JVM) can execute.

3.3 Running Java programs

Use the command line tool to run the compiled Java program:

java HelloWorld

The output should be:

Hello, World!

4. Frequently Asked Questions and Solutions

4.1 javaorjavacCommand not found

If it is executedjava -versionorjavac -versionThe error "Command Not Found" may be that the environment variable is not configured correctly.

examinePATHandJAVA_HOMEAre the environment variables set correctly and make sure to restart the command line tool or executesourceThe command enables the configuration.

4.2 Compilation errors

If an error occurs during compilation, check whether the code has syntax errors and whether the file name is consistent with the class name.

For example, the class name isHelloWorldThe Java file should be named

4.3 Runtime Error

If an error occurs during the run, check whether the Java file is compiled correctly and whether it is executed in the correct directory.javaCommand, whether the class name is spelled correctly.

in conclusion

Correctly configuring the Java development environment is the first step in Java development. Through the detailed introduction of this article, readers should be able to install and configure JDK on different operating systems and be proficient in writing, compiling, and running Java programs.

Whether a novice developer or a senior engineer, mastering these basic skills will lay a solid foundation for subsequent Java development.

With the development of technology, the Java ecosystem is also constantly evolving. Maintaining attention to new versions and new features will help improve development efficiency and code quality.

The above is personal experience. I hope you can give you a reference and I hope you can support me more.