How To Create, Debug Java Project In Visual Studio Code

Visual studio code is an open source lightweight code editor provided by Microsoft. It is not a heavyweight IDE, it is just a platform and startup very quickly. But it can support any coding languages develop and debugging through it’s extension. And there are so many extensions in visual studio code marketplace. And as time goes by, there will have more and more extensions added.

In single words, it is not a java IDE, but if you want to develop and debug java application, only need to install related java extensions. This applies to any other coding languages such as C++, Python etc. This article will show you how to use it to develop and debug java application.

1. Install Visual Studio Code.

Install visual studio code is very easy, just go to visual studio code download page to download related OS version, then just install it as normal by click.

2. Install Java Project Needed Extensions.

Before you can develop and debug java application in visual studio code, you need to add below extensions in 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 in left.
    visual studio code view extensions menu item
  3. Input keyword maven for java in the search box, it will list related extensions at below. If the extension is not installed, there will have a green Install button at extension right bottom. Click the Install button to install it. Follow same method to install other extensions.
    install visual studio code maven for java extension

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 mvn binary executable file is saved. Below step is for MacOS, it is similar for Windows and Linux. Of course first you need to confirm maven has been installed on your MacOS. You can refer 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 Java configurations menu item under Extensions, and input the maven settings.xml file absolute full path in the input box.
  4. Click Maven for Java menu item and input maven executable file absolute full path in Maven > Executable:Path input box.
  5. All the configuration will be saved automatically after you input them.
READ :   How To Use JUnit Annotations In Java Project

4. Create Java Project With Maven Archetype.

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

  1. Create a folder named VisualStudioCodeWorkSpace.
  2. Open visual studio code, then click File —> Open and select above folder.
  3. Then click View —> Command Palette… menu item or press Command and 3 button at same time. It will open a command input text box at top.
  4. Input keyword maven in the text box, and select Maven:Generate from Maven Archetype item in the dropdown list.
    run maven archetype command in command input box
  5. After a while, it will list all maven archetype for you to choose, just input keyword jdk in the text box, and select archetype-quickstart-jdk8 in the list.
    select maven archetype quickstart jdk8
  6. Then the maven command will execute and download related jars from maven repository, you can see all this interaction process log information in the terminal area at visual studio code bottom panel. If it prompt you to input something such as group id, artifact id, just input them. This process may take a little longer, please be patient.
    maven quickstart jdk8 install process console logs
  7. When the build process success, you can find a folder VSJavaDebugExample ( artifactId name ) under the workspace folder. And there are src and pom.xml files in it.
  8. You can also create the java project use Yeoman tool, you can refer How To Use Yeoman To Scaffold Java Spring Project.

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

Now the java project has been created, you can find the java project source files in left files list panel. If you want to develop and debug an exist java project, just click File —> Open and select the exist java project folder.

READ :   Java Compare Date Example

visual studio code java project app java file

Below is the steps to debug the java application.

  1. Click file to open it in right panel.
  2. Click the beginning of each code line to enable/disable a break point. When you see the red point, it means the break point is enabled.
  3. Click Debug —> Start Debugging menu item to start debugging the file. The Debug —> Start Without Debugging menu item will just run the java app without stop at the breakpoint.
    visual studio code debug start debugging menu item
  4. If you encounter error message which tell you 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 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, string ‘Hello World!’ will be printed in DEBUG CONSOLE.

6. Run Maven Task.

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

visual studio code run maven task on maven java project

Leave a Reply

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.