Caution: VS Code guides section is a work-in-progress, containing content contributed by students.
This guide will walk you through creating a new Java project in VS Code from scratch.
Need help with following prerequisites? Check out our Preparing VS Code for Java guide first.
There are two methods you can use to create a new Java project:
The easiest way to create a new Java project is using VS Code's built-in project wizard.
Ctrl+Shift+P
| Cmd+Shift+P
Java: Create Java Project
and select it
If you prefer more control over the project setup, you can create a project manually.
Step 1: Create a workspace folder
MyJavaProject
)File
→ Open Folder...
Step 2: Set up the project structure
Create the following simple folder structure:
MyJavaProject/
└── src/
└── (your Java source files will go here)
Open the Explorer panel: / Ctrl+Shift+E
| *md+Shift+E`
Right-click in the Explorer panel
Select New Folder
Name it src
VS Code automatically handles compilation, so you do not need separate folders for compiled .class
files.
Step 3: Create your first Java class
Right-click on the src
folder
Select New File
Name it HelloWorld.java
Add the following code:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Save the file: / Ctrl+S
| Cmd+S
VS Code will automatically compile the Java file when you save it.
If you already have Java installed, VS Code should automatically detect and configure it when you create your project.
After creating your project, you may need to verify and configure VS Code to use the correct JDK version.
Ctrl+Shift+P
| Cmd+Shift+P
Java: Configure Java Runtime
and select itTo view and modify project settings:
Ctrl+Shift+P
| Cmd+Shift+P
Java: Open Project Settings
and select it.class
files will be stored.vscode/settings.json
You can also configure project settings by creating a .vscode/settings.json
file in the root directory:
{
"java.project.sourcePaths": [
"src"
],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}
If you are using Git to version control the project, you may want to add the following to your .gitignore
file:
# Compiled class files
*.class
# VS Code workspace settings
.vscode/
main
methodmain
method or in the top right corner of the editorContributors: John Wong (@Johnwz123)