How to Create a Java Project in Visual Studio Code (VS Code)

If you're just getting started with Java development and looking for a lightweight yet powerful code editor, Visual Studio Code (VS Code) is an excellent choice. Unlike traditional IDEs, VS Code offers a clean, modern interface, enhanced by extensions that make it incredibly versatile for Java programming.

This tutorial will provide step-by-step instructions for creating your first Java project in VS Code. Whether you're a beginner exploring Java for the first time or an experienced developer looking for a flexible coding environment, this guide has you covered.

Here’s what you’ll learn:

  • How to set up VS Code for Java.
  • How to create, write, and run a Java program.
  • Tips for debugging and organizing your Java projects.

By the end of this tutorial, you’ll be able to confidently build and run Java programs using VS Code.

YouTube Video

Prerequisites

To follow along with this guide, you’ll need:

  1. Java Development Kit (JDK): Ensure you have installed JDK 23 or later. You can check this by opening a terminal and running:

    java -version
    

    If you don’t have the JDK installed, check this guide.

  2. Visual Studio Code: To install VS code, use this guide.

  3. Java Extensions for VS Code: We’ll install these as part of the guide, so you’re all set if you don’t have them yet.

Step 1: Install Java Extensions in VS Code

  1. Open Visual Studio Code.
  2. Go to the Extensions view by clicking on the Extensions icon in the left sidebar or pressing Ctrl+Shift+X.
  3. Search for Java Extension Pack and click Install.
    • This extension pack includes tools for IntelliSense, debugging, and managing Java projects.
  4. Optionally, you can install additional extensions like:
    • Debugger for Java: For debugging Java applications.
    • Test Runner for Java: For testing Java code.

Step 2: Create a New Java Project

Option 1: Using the Java Extension Pack

  1. Open the Command Palette by pressing Ctrl+Shift+P.
  2. Type and select Java: Create Java Project.
  3. Choose a project type:
    • No Build Tools: Best for simple projects.
    • Maven: If you plan to use dependencies.
    • Gradle: For advanced build automation.
  4. Select a folder to save your project and enter a project name (e.g., MyJavaProject).
  5. VS Code will automatically generate the project structure.

Option 2: Manual Setup

  1. Create a new folder (e.g., MyJavaProject) and open it in VS Code by clicking File > Open Folder.
  2. Inside the folder, create a subfolder named src.
  3. Inside src, create a file named Main.java.

Step 3: Write Your First Java Program

  1. Open the Main.java file in the src folder.
  2. Add the following code:
    public class Main {
        public static void main(String[] args) {
            System.out.println("Hello, Visual Studio Code!");
        }
    }
    
  3. Save the file by pressing Ctrl+S.

Step 4: Run the Java Program

Option 1: Using the Run Button

  1. A green Run button should appear at the top-right corner of the editor. Click it to execute the program.
  2. The output will appear in the Terminal at the bottom of VS Code.

Option 2: Using the Terminal

  1. Open the terminal in VS Code by pressing `Ctrl+`` (backtick).
  2. Navigate to the src folder:
    cd src
    
  3. Compile the program:
    javac Main.java
    
  4. Run the program:
    java Main
    
  5. You should see the output:
    Hello, Visual Studio Code!

Step 5: Debug the Java Program (Optional)

Debugging helps you identify and fix issues in your code. Here’s how to debug in VS Code:

  1. Open the Run and Debug view by clicking the bug icon in the left sidebar or pressing Ctrl+Shift+D.
  2. Click Create a launch.json file and select Java as the environment.
  3. Add breakpoints by clicking to the left of the line numbers in the editor.
  4. Start debugging by clicking the green Start Debugging button.
  5. You can inspect variables, step through code, and view the program flow in the Debug Console.

Step 6: Organize Your Project with Packages (Optional)

For better organization, especially in larger projects:

  1. Inside the src folder, create a new folder (e.g., com/example).
  2. Move Main.java into this folder and update the package declaration:
    package com.example;
    
    public class Main {
        public static void main(String[] args) {
            System.out.println("Hello, Visual Studio Code!");
        }
    }
    
  3. Compile and run the program as before.

Step 7: Add Dependencies (Optional)

If your project requires additional libraries:

  1. Use Maven or Gradle as your build tool.
  2. Add dependencies to the pom.xml (for Maven) or build.gradle (for Gradle).
  3. Run mvn compile (for Maven) or gradle build (for Gradle) in the terminal to fetch and use dependencies.

Troubleshooting

  • Java Not Found: Ensure the JAVA_HOME environment variable is set and points to your JDK installation.
  • IntelliSense Not Working: Restart VS Code or ensure the Java Extension Pack is enabled.
  • Compilation Errors: Double-check the code syntax and file structure.

Conclusion

Congratulations! You’ve successfully created and run your first Java project in Visual Studio Code. With its flexibility and rich ecosystem of extensions, VS Code is an excellent choice for Java developers. Keep exploring its features, and as your projects grow, consider integrating build tools like Maven or Gradle for easier dependency management. Happy coding!

Comments