How To Create, Debug Java Project In Visual Studio Code

Visual studio code is an open-source lightweight code editor provided by Microsoft. It can startup very quickly. It can support any coding language development and debug through its extension. There are so many extensions in the visual studio code marketplace.

In single words, visual studio code is not a java IDE, but it can be used to develop and debug Java projects by install related java extensions in it. If you want to use visual studio code to develop and debug C++ or Python programs, you can install related extensions in it to achieve the goal. This article will show you how to use visual studio code to develop and debug a java project.

1. Install Visual Studio Code.

Install visual studio code is very easy, just go to the visual studio code download page to download the related OS version, then just install it as normal by clicking the installer or unzip it to use directly.

2. Install Visual Studio Java Project Extension.

2.1 Install Visual Studio Java Project Extensions Manually.

Before you can use visual studio to develop and debug a java project, you need to add the below extensions to it.

  1. Language Support for Java(TM) by Red Hat.
  2. Debugger for Java.
  3. Maven for Java.
  4. Java Extension Pack.
  5. Java Test Runner.

You should install them one by one. Now I just demo how to install Maven for Java extension, other extension installation is similar.

  1. Open visual studio code.
  2. Click View —> Extensions menu item in the top menu bar to open the extension panel on the left. It will list all the INSTALLED and RECOMMENDED extensions in the EXTENSIONS panel.
  3. Input keyword maven for java in the search box, it will list related extensions below. If the extension is not installed, there will have an Install button at the extension right bottom corner. Click the Install button to install it. Follow the same method to install other extensions.
    install-visual-studio-code-maven-for-java-extension

2.2 Install Visual Studio Java Project Extensions With One Click.

  1. Visual studio code provides a shortcut for you to install the visual studio java project extensions with a single click.
  2. Open Microsoft visual studio, click the Help —> Welcome menu item, it will open the visual studio welcome page.
  3. Click the Java link under the Customize section on the visual studio code welcome page right side. Then it will install all the java extensions which you need when you use visual studio to develop and debug a java project.
    visual studio code welcome page customize section

3. Configure Maven Tool Settings.

Because we want to use maven to manage(create, build, compile, etc) our java project. We should tell visual studio code where the maven settings.xml file and the mvn binary executable file are saved. The below step is for macOS, it is similar for Windows and Linux. First, you need to confirm maven has been installed on your macOS. You can refer to Create Java Project With Maven to learn more.

  1. Click Code —> Preferences —> Settings menu item in visual studio code.
    macos-code-preferences-settings-menu-item
  2. Input keyword maven in the Settings search box and there will list maven related items.
    edit-maven-configuration-in-visual-studio-code-settings-panel
  3. Click the Java menu item under Extensions on the left side, and input the maven settings.xml file absolute full path on the right side input box under Java > Configuration > Maven: User Settings item.
  4. Click Maven for Java menu item on the left side and input maven executable file absolute full path in Maven > Executable:Path input box on the right side.
  5. All the configurations will be saved automatically after you input them.

4. Create Visual Studio Java Project With Maven Archetype.

Before we can edit and debug java code, we need to use visual studio to create a java project with the maven archetype as below.

  1. Create a folder named VisualStudioCodeWorkSpace.
  2. Open visual studio code, then click File —> Open and select the above folder.
  3. Then click View —> Command Palette… menu item. It will open a command input text box at the top.
  4. Input keyword maven in the text box, and select Maven: Create Maven Project item in the popup dropdown list.
  5. After a while, it will list all maven archetypes for you to choose from, input the keyword jdk in the search text box, and select archetype-quickstart-jdk8 in the list.
    select-maven-archetype-quickstart-jdk8
  6. Choose a version in the next version drop-down list.
  7. Select a target folder in the browse directory popup window and click the Select Destination Folder button.
  8. Then the maven command will execute and download related jars from the maven central repository, you can see all this interaction process log information in the terminal area at the visual studio code bottom panel.
  9. If it prompts you to input something such as groupId, artifactId, version, etc just input them. This process may take a little longer, please be patient.
    maven-quickstart-jdk8-install-process-console-logs
  10. When the build process completes successfully, you can find a folder like VSJavaDebugExample ( artifactId name ) under the workspace folder. And there are src and pom.xml files in it.
  11. You can also create the java project use the Yeoman tool, you can read the article How To Use Yeoman To Scaffold Java Spring Project.

5. Develop And Debug Visual Studio Java Project In Visual Studio Code.

5.1 How To Open The Created Or Existing Visual Studio Java Project.

  1. Open visual studio code, click the View —> Explorer menu item at the top menu bar.
  2. You can see there has an open a Java project link and Create Java Project button at the end of the EXPLORER.
    visual studio - explorer - open a java project - create java project-1
  3. Click the open a Java project link to open the java project which we just created.
  4. You can find the visual studio java project source files in the left side list panel.

5.2 How To Debug Visual Studio Java Project.

  1. Click the App.java file in the visual studio java project left side explorer to open and edit it on the right side.
  2. Click the beginning of each code line to enable/disable a breakpoint. When you see a red color point, it means the breakpoint is enabled.
  3. Click Run —> Start Debugging menu item to start debugging the file. The Run —> Start Without Debugging menu item will just run the java app without stopping at the breakpoint.
  4. If you encounter an error message which tells you the build failed, you can click the PROBLEMS tab to see what problem occurred. If there has no problem, then just click Proceed button to continue the debug process. Click the Learn More button will open the error resolution page.
    visual-studio-code-debug-java-project-build-error
  5. If everything went well, the string ‘Hello World!’ will be printed on the TERMINAL.

6. Run Maven Task.

Because you had added visual studio code Maven for Java extension, so there is a MAVEN tab in the left panel. Right-click the M file, you can run any maven task in the popup menu list. For example, we run a maven package task, after the task complete, a jar file will be created in the WorkSpace / Java_Project / target folder.

visual-studio-code-run-maven-task-on-maven-java-project

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.