introduction
As a programming language widely used in various fields of software development, Java is an important starting point for beginners to quickly build a development environment and run the first program. This article will introduce a simple way to install, configure the Java environment, and write and run the first Java program with Visual Studio Code (VSCode for short).
1. Java environment installation
1. Download and install JDK
JDK (Java Development Kit) is a core toolkit for Java development, including necessary components such as Java compiler, Java virtual machine, etc. We canAdoptiumDownload the OpenJDK distribution that suits your operating system. Here, take the Windows system as an example, the download and installation steps are as follows:
- Open the Adoptium official website and select the JDK version (such as Java 17) that suits your system and the corresponding operating system (Windows) and architecture (x64).
- After the download is completed, double-click the installation package and follow the prompts of the installation wizard to complete the installation. During the installation process, it is recommended to select the default installation path to facilitate subsequent configuration.
2. Verify the JDK installation
After the installation is completed, you need to verify that the JDK is installed successfully. Open the command prompt (pressWin + R
, entercmd
and enter the following command in the command prompt:
java -version javac -version
If the version information of Java and Java compilers are displayed respectively, it means that the JDK installation is successful.
2. VSCode installation and configuration
1. Download and install VSCode
VisitVSCode official website, download the installation package suitable for your operating system, and follow the installation wizard to complete the installation.
2. Install Java extensions
Open VSCode, click the extension icon (the icon composed of four squares) on the left, enter the following extension in the search box and install:
- Extension Pack for Java: This is a Java extension package that contains multiple extensions required for Java development, such as Java language support, debugger, etc.
- Java Test Runner: Convenient to Java unit testing.
- Maven for Java: This extension will be useful if Maven project development is involved in subsequent development.
3. Configure the Java environment of VSCode
After installing the extension, VSCode will automatically detect the JDK installed in the system. If it is not automatically detected, you can manually configure it through the following steps:
- Open the command panel (press
Ctrl + Shift + P
), enterJava: Configure Java Runtime
And press Enter. - In the pop-up window, click
Add JDK
, select the installation path of JDK.
3. Write the first Java program
1. Create a project folder
Create a new folder on the local disk as your Java project folder, e.g.JavaFirstProgram
。
2. Open the project folder
Open VSCode and clickdocument
-> Open a folder
, select the one you just createdJavaFirstProgram
Folder.
3. Create Java files
In VSCode's Explorer, right-click the folder and selectCreate a new file
, named。
4. Write Java code
existEnter the following code into the file:
public class HelloWorld { public static void main(String[] args) { ("Hello, Java World!"); } }
Code explanation:
-
public class HelloWorld
: Define a public class, the class name isHelloWorld
, Java requires that the file name and the class name must be the same. -
public static void main(String[] args)
: This is the entry method of a Java program, and the program starts executing from here. -
("Hello, Java World!");
: Used to output the string "Hello, Java World!" in the console.
4. Compile and run Java programs
1. Compile and run
existIn the file editing interface, click the green triangle icon (run button) in the upper right corner, or press
Ctrl + F5
Key combination. VSCode will automatically compile and run the program, and output the result in the terminal window:
Hello, Java World!
2. Use the command line method (optional)
You can also compile and run Java programs using the command line. Open the terminal of VSCode (pressCtrl +
), enter the following command in the terminal:
- Compilation:
javac
- run:
java HelloWorld
5. Summary
Through the above simple steps, we completed the installation of the Java environment, configured VSCode as a Java development tool, and wrote and ran the first Java program. VSCode provides a convenient environment for Java development with its rich extensions and simple interface. I hope this article can help beginners quickly get started with Java programming and start their programming journey.
This is the article about installing, writing and running the first program of Java environment. For more information about installing and writing the first program of Java environment, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!