Setting up Java with Gradle in VS Code
Here's how to set up Java with Gradle in Visual Studio Code:
1. Install Prerequisites
-
Install Java Development Kit (JDK)
- Download and install an LTS version (8, 11, 17, or 21) from Oracle or Adoptium
-
Install Gradle
-
Download from Gradle website
- Extract the archive to a location on your computer
- Add Gradle's
bindirectory to your system PATH - Verify installations in a terminal/command prompt:
java -version gradle -version
2. Install VS Code Extensions
- Open VS Code and go to Extensions (Ctrl+Shift+X)
- Install these extensions:
- "Extension Pack for Java" (includes Java essentials)
- "Gradle for Java" (if not included in the pack)
3. Create a Gradle Project
Option 1: Using VS Code Command Palette
- Press Ctrl+Shift+P to open the Command Palette
- Type "Gradle: Create Gradle Project"
- Follow the prompts to set up your project
Option 2: Using Terminal in VS Code
- Open a terminal in VS Code (Ctrl+`)
- Navigate to your desired directory
-
Run:
gradle init -
Select options when prompted:
- Type of project: "application"
- Implementation language: "Java"
- Build script DSL: "Groovy" (or "Kotlin")
- Test framework: "JUnit Jupiter" (recommended)
- Project name: your choice
- Source package: e.g., "com.example.app"
4. Working with Your Gradle Project
- The Gradle Explorer view shows your project structure
- Edit
build.gradleto manage dependencies and build configuration - Key files:
build.gradle(project configuration)settings.gradle(project settings)gradlewandgradlew.bat(Gradle wrapper scripts)
5. Running Gradle Tasks
-
Open the Gradle Tasks view in the Explorer
-
Expand your project to see available tasks
-
Common tasks:
build: Assembles and tests the projectclean: Removes build directoriesrun: Runs the application-
Run tasks by:
-
Clicking on them in the Gradle Tasks view
- Using the Command Palette: "Gradle: Run Gradle Task"
- Terminal command:
./gradlew taskName(orgradlew taskNameon Windows)
6. Running Your Application
- Use the Gradle "run" task
- Or open the Java file with your main method and click the "Run" button
- Alternatively, use the Command Palette: "Java: Run Java"
Your project is now set up with Java and Gradle in VS Code!