Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing java an argument while debugging in VS Code

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.

like image 292
RobG Avatar asked Jan 08 '20 03:01

RobG


People also ask

How to debug Java code in Visual Studio Code?

Running and debugging Java Visual Studio Code allows you to debug Java applications through the Debugger for Java extension. It's a lightweight Java debugger based on Java Debug Server, which extends the Language Support for Java by Red Hat. Here's a list of supported debugging features:

How to start debugging or launch a code file by passing arguments?

And how 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 Configuratio or Run -> Add Configuration

Is there a way to debug arguments in C++ using VS Code?

Type: Debugger In the Java extension for VS Code, when editing launch parameters, you can set args to be "$ {command:SpecifyProgramArgs}" which will prompt for arguments during launch the debugger. Having such a feature for C++ would be great.

How do I run a program in VS Code without debugging?

In addition to debugging a program, VS Code supports running the program. The Debug: Run (Start Without Debugging) action is triggered with Ctrl+F5 and uses the currently selected launch configuration. Many of the launch configuration attributes are supported in 'Run' mode.


3 Answers

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

like image 145
MadaManu Avatar answered Sep 21 '22 10:09

MadaManu


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

like image 44
Joshua Pereira Avatar answered Sep 18 '22 10:09

Joshua Pereira


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"
}
like image 33
Eric Manley Avatar answered Sep 20 '22 10:09

Eric Manley