Visual Studio Code (VS Code) is a lightweight and versatile code editor that is popular among developers. It is also a great choice for writing and running Java programs. In this tutorial, we will write our First Java Program and run it using VScode.
To write your first Java program in VS Code, you need to follow these steps
- Install Java Development Kit (JDK) on your computer
To write and run Java programs, you need to install JDK on your computer. You can download the latest version of JDK from the Oracle website: https://www.oracle.com/java/technologies/javase-downloads.html.
- Install the Java Extension Pack in VS Code
The Java Extension Pack provides essential tools for Java development in VS Code, such as language support, debugging, and code completion. You can install it from the VS Code Marketplace: https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-pack.
- Create a new Java project in VS Code
To create a new Java project in VS Code, follow these steps
- Open VS Code and click on File > Open Folder.
- Create a new folder for your project and select it.
- Click on the New File button in the Explorer pane and create a new file with the .java extension (e.g., HelloWorld.java).
- Write your first Java program
To write your first Java program in VS Code, follow these steps
- Open the HelloWorld.java file in the VS Code editor.
- Type the following code
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
This program will print “Hello, World!” to the console.
- Compile and run your Java program
To compile and run your Java program in VS Code, follow these steps
- Open the Terminal in VS Code (View > Terminal).
- Compile your Java program by typing the following command:
javac HelloWorld.java
. - Run your Java program by typing the following command:
java HelloWorld
.
You should see “Hello, World!” printed to the console.
Congratulations! You have written and run your first Java program in VS Code. From here, you can continue learning Java by exploring its syntax, data types, control structures, and object-oriented programming concepts. Happy coding!