Java新手求助:如何在Visual Studio Code的Java项目中创建包
Hey there! Let's get this sorted out for you. Since you're new to Maven/Spring Boot and transitioning to VS Code from Eclipse/IntelliJ, creating Java packages might feel a bit different at first—no worries, it's totally straightforward once you know the steps.
Here are 3 easy ways to create Java packages in your Eclipse project:
1. Create via File Explorer (Right-Click)
- Open the File Explorer (the first icon on the left sidebar in VS Code)
- Locate the source folder in your Eclipse project—this is usually
src/main/javafor standard projects, or justsrcfor simpler Java projects - Right-click on this source folder and select New Folder
- Type your package name using dot separators (e.g.,
com.example.myapp)—VS Code will automatically convert this into nested folders (which is exactly what a Java package is under the hood; Eclipse just displays it as a single "package" entry instead of separate folders) - You can now right-click the new package folder to create Java classes inside it
2. Use the Command Palette
- Press
Ctrl+Shift+P(Windows/Linux) orCmd+Shift+P(Mac) to open the Command Palette - Start typing
Java: Create Packageand select the matching command when it appears - Enter your desired package name (e.g.,
com.example.demo) in the pop-up input box - Select the target source folder (like
src/main/java) from the list, and VS Code will generate the package structure automatically
3. Create a Class and Define the Package at the Same Time
- Right-click your source folder and choose New File
- When naming the file, include the package path with slashes instead of dots (e.g.,
com/example/myapp/HelloWorld.java) - VS Code will create all the intermediate package folders for you, and the generated Java file will automatically include the correct
package com.example.myapp;declaration at the top
Quick Note:
VS Code handles Java packages just like Eclipse does—they're just nested folders under the hood. The only difference is how they're displayed: Eclipse collapses nested folders into a single "package" entry, while VS Code shows the full folder structure by default. This doesn't affect functionality at all!
If you later want to experiment with Maven or Spring Boot projects, you can use commands like Java: Create Maven Project or Spring Initializr: Create a Maven Project from the Command Palette to set those up quickly.
内容的提问来源于stack exchange,提问作者Ultron

