Passing java an argument while debugging in VS Code

11,135

Solution 1

Assuming you have the Java Extension Pack installed.

Once you attempt to run the java file containing your main, you should see a file launch.json generated. If you open it with the editor, you will then be able to add multiple flags into the run configurations.

One of the flags that you can add is vmArgs And vmArgs according to their docs:

vmArgs - The extra options and system properties for the JVM (for example -Xms -Xmx -D=), it accepts a string or an array of string.

More info: visualstudio docs

Solution 2

Not sure if this solution is applicable if you're passing vmArgs in launch.json directly, but I ran into trouble when using settings.json

When you go to Settings > Launch in VSCode, it brings you to settings.json, where it automatically adds the following boilerplate:

"launch": {
    "configurations": [],
    "compounds": []
}

Following the documentation, I modified it with the necessary vmArgs to launch my application:

"launch": {
    "configurations": [
        {
            "type": "java",
            "request": "launch",
            "vmArgs": "--foo arg1 --bar arg2",
            "mainClass": "baz.Klass",
            "projectName": "baz"
        }
    ],
    "compounds": []
}

What I didn't know is that this configuration gets copied over to launch.json when attempting to run the application. It appears launch.json is what's actually used when configuring the app, however, not every entry gets copied over properly (notably vmArgs).

Before attempting to launch my app, there was no launch.json file in my .vscode directory. After attempting to launch my app, the launch.json file was created with the following content:

{
    "configurations": [
        {
            "type": "java",
            "request": "launch",
            "mainClass": "baz.Klass",
            "projectName": "baz_ca302835"
        },
        {
            "type": "java",
            "request": "launch",
            "vmArgs": "--foo arg1 --bar arg2",
            "mainClass": "baz.Klass",
            "projectName": "baz"
        }
    ] 
}

I simply had to copy the vmArgs from the second configuration object to the first configuration object, and the application was successfully invoked with the proper command line arguments.

If you check your terminal and see that the vmArgs aren't present when your app is invoked, this solution might be applicable, as this was the case for me before I copied them into the first configuration object in launch.json

Solution 3

For me, it was adding the args array to the config as below.

This passes in "-G" as the argument.

{
  "type": "java",
  "name": "Launch CommandLineController",
  "request": "launch",            
  "args": 
  [
      "-G"
  ],            
  "mainClass": "com.eric.controller.CommandLineController",      
  "projectName": "quotes"
}
Share:
11,135
RobG
Author by

RobG

My "real job" is working nights at a semiconductor factory, as a repair & maintenance tech supporting the probe/test area. My "wannabe job" is doing freelance web and desktop application development, mainly for non-profits. My proudest accomplishment (that's a joke) is a program I wrote using Clipper (a compiler for xBase code) used by a local restaurant; that program has been in service since 1995.

Updated on June 08, 2022

Comments

  • RobG
    RobG about 2 years

    I need to pass an argument to the java command when running my Java program. This argument is "-Dderby.system.home=D:\DataDir", telling Java where the Derby database is located. In Eclipse I can simply add the argument in the Run Configuration, but how to do this in VS Code eludes me. Any help will be greatly appreciated.