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.
- Language Support for Java(TM) by Red Hat
- Debugger for Java
- Maven for Java
- Java Extension Pack
- 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.
- Open visual studio code.
- Click View —> Extensions menu item in the top menu bar to open the extension panel in left.
- 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.
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.
- Click Code —> Preferences —> Settings menu item in visual studio code.
- Input keyword maven in the Settings search box, and there will list maven related items.
- Click Java configurations menu item under Extensions, and input the maven settings.xml file absolute full path in the input box.
- Click Maven for Java menu item and input maven executable file absolute full path in Maven > Executable:Path input box.
- All the configuration will be saved automatically after you input them.
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.
- Create a folder named VisualStudioCodeWorkSpace.
- Open visual studio code, then click File —> Open and select above folder.
- 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.
- Input keyword maven in the text box, and select Maven:Generate from Maven Archetype item in the dropdown list.
- 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.
- 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.
- 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.
- 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.
Below is the steps to debug the java application.
- Click App.java file to open it in right panel.
- 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.
- 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.
- 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.
- 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.