Java 21 Download and Installation on Windows: A Comprehensive Guide
Java is a widely used programming language known for its platform - independenceobject-oriented natureand vast standard library. Java 21 is the latest long-term support (LTS) release of the Java Development Kit (JDK)which comes with several new features and improvements. This blog will guide you through the process of downloading and setting up Java 21 on a Windows operating systemand also cover its usagecommon practicesand best practices.
Table of Contents#
- Fundamental Concepts of Java 21 on Windows
- Downloading Java 21 on Windows
- Installing Java 21 on Windows
- Verifying the Installation
- Usage Methods
- Common Practices
- Best Practices
- Conclusion
- References
1. Fundamental Concepts of Java 21 on Windows#
What is Java 21?#
Java 21 is an important milestone in the Java ecosystem. It includes features such as Virtual Threadswhich are lightweight threads that can greatly improve the performance of applications handling a large number of concurrent tasks. It also has improvements in the standard library and language syntax enhancements.
Why use Java 21 on Windows?#
Windows is one of the most popular operating systems in the world. By using Java 21 on Windowsdevelopers can take advantage of the latest Java features while leveraging the familiar Windows environment for developmenttestingand deployment.
2. Downloading Java 21 on Windows#
Step 1: Visit the Oracle or OpenJDK Website#
- Oracle: You can visit the official Oracle JDK 21 download page at Oracle Java Downloads. You may need to create an Oracle account if you are downloading the Oracle JDK.
- OpenJDK: For OpenJDKa popular open-source alternativeyou can visit Adoptium. It provides pre-built OpenJDK binaries for different operating systemsincluding Windows.
Step 2: Select the Appropriate Version#
On both Oracle and Adoptium websitesyou need to select the Windows version of Java 21. Usuallythere are options for 32 - bit and 64 - bit systems. Make sure to choose the one that matches your Windows installation.
Step 3: Start the Download#
After selecting the correct versionclick the download button. The download may take some time depending on your internet speed.
3. Installing Java 21 on Windows#
Step 1: Locate the Downloaded File#
Once the download is completefind the installer file in your Downloads folder. It will typically have a .exe extension.
Step 2: Run the Installer#
double-click on the installer file. A setup wizard will appear. Follow the on-screen instructions. You can usually accept the default settingsbut you may want to change the installation directory if you prefer.
Step 3: Complete the Installation#
After following all the steps in the setup wizardclick the "Finish" button to complete the installation.
4. Verifying the Installation#
Open the Command Prompt (you can search for "Command Prompt" in the Start menu). In the Command Prompttype the following commands:
java -version
javac -versionIf the installation was successfulyou should see output indicating the version of Java 21similar to the following:
java version "21" 2023 - 09 - 19 LTS
Java(TM) SE Runtime Environment (build 21+35 - LTS - 2513)
Java HotSpot(TM) 64 - Bit Server VM (build 21+35 - LTS - 2513mixed modesharing)
javac 21
5. Usage Methods#
Writing a Simple Java Program#
Create a new text file and name it HelloWorld.java. Open the file in a text editor and add the following code:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("HelloWorld!");
}
}Compiling and Running the Program#
Open the Command Prompt and navigate to the directory where the HelloWorld.java file is located using the cd command. Thencompile the program using the javac command:
javac HelloWorld.javaThis will create a HelloWorld.class file. To run the programuse the java command:
java HelloWorldYou should see the output HelloWorld! in the Command Prompt.
6. Common Practices#
Project Structure#
For larger Java projectsit is a common practice to follow a standard project structure. For exampleusing the Maven or Gradle build toolsthe project structure might look like this:
project - root/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/
│ │ │ └── example/
│ │ │ └── YourClass.java
│ │ └── resources/
│ │ └── config.properties
│ └── test/
│ ├── java/
│ │ └── com/
│ │ └── example/
│ │ └── YourClassTest.java
│ └── resources/
│ └── test - config.properties
├── pom.xml (for Maven) or build.gradle (for Gradle)
Error Handling#
In Javait is important to handle exceptions properly. For example:
import java.io.FileInputStream;
import java.io.IOException;
public class ExceptionExample {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("nonexistentfile.txt");
} catch (IOException e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}7. Best Practices#
Using Virtual Threads#
Java 21 introduced virtual threadswhich can significantly improve the performance of concurrent applications. Here is an example of using virtual threads to perform multiple tasks concurrently:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class VirtualThreadExample {
public static void main(String[] args) {
try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
for (int i = 0; i < 1000; i++) {
final int taskId = i;
executor.submit(() -> {
System.out.println("Task " + taskId + " is running on thread " + Thread.currentThread());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("Task " + taskId + " is completed.");
});
}
}
}
}Code Readability and Documentation#
Write cleanreadable code and add Javadoc comments to your classesmethodsand fields. This will make your code more maintainable and easier for other developers to understand.
8. Conclusion#
Downloading and using Java 21 on Windows is a straightforward process. By following the steps in this guideyou can get Java 21 up and running on your Windows machine. Additionallyunderstanding the common and best practices will help you write more efficient and maintainable Java code.
9. References#
- Oracle Java 21 Downloads: https://www.oracle.com/java/technologies/javase/jdk21-downloads.html
- Adoptium: https://adoptium.net/
- The Java Tutorials: https://docs.oracle.com/javase/tutorial/