VS-Code How to add command-line arguments for Debugging

Introduction

Sooooo, learning VS-Code the hard-way, trying to debug a project, I finally figured out how to add the equivalent of command-line args into the debug-configuration of VS-Code.

In this post, I will take you through how to add command Iine arguments in an example Java project. Once completed, we can start debugging or launch a code file by passing command line arguments.

For this, you would need launch.json. If you have, just edit it as I will discuss in this post. To create a new launch.json, click on:

Run -> Open Configuration or Run -> Add Configuration.

launch.json:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [

        {
            "type": "java",
            "name": "Launch Current File",
            "request": "launch",
            
            "mainClass": "${file}"
        },
        {
            "type": "java",
            "name": "Launch CommandLineController",
            "request": "launch",            
            "args": 
            [
                "-G"
            ],            
            "mainClass": "com.eric.controller.CommandLineController",      
            "projectName": "quotes"
        }
    ]
}

In the above file, we’re telling VS-Code to launch the current class CommandLineController in the project quotes, with the argument “-G”.

Please note, that in my searching, I found several examples that used “arguments”, instead of “args” as the name of the array. My version of VS-Code, would not accept this!

Leave a comment