6

Is there a way I can set java VM arguments at workspace level? That is, not having to have it in each launch configuration in the launch.json. A specific argument I want to be used whenever a launch is triggered for the current workspace in vscode. I also don't want it to be global to all Java projects opened in vscode, just the current workspace.

I tried adding this in .vscode/settings.json, but it doesn't seem to use it.

{
  "java.jdt.ls.vmargs": "-javaagent:/Users/me/my-agent.jar"
}

Any suggestions? Ways to achieve this?

Johannes Kuhn
  • 14,778
  • 4
  • 49
  • 73
Shabirmean
  • 2,341
  • 4
  • 21
  • 34
  • Related vscode repo link: https://github.com/microsoft/vscode/issues/97363 where the same question is raised – Shabirmean May 11 '20 at 16:28

2 Answers2

6

You should use vmArgs option in the .vscode/launch.json file, create an entry for java launcher configuration as below -

    {
        "version": "0.2.0",
        "configurations": [

            {
                "type": "java",
                "name": "Debug (Launch) with Arguments Prompt",
                "request": "launch",
                "mainClass": "com.myapp.Main",
                "args": "${command:SpecifyProgramArgs}",
                "vmArgs" : "${command:SpecifyVMArgs}"
            }
        ]
        }

You can get the list of other options here https://code.visualstudio.com/docs/java/java-debugging#_launch.

user51
  • 8,843
  • 21
  • 79
  • 158
  • This works fine. However, my requirement is different. What if I have multiple launch configurations and I want a certain vmArg to be in all of them? With the above as far as I know I will have to add the vmArg to all of them. What I am looking for is a way to add it at one levvel above so the arg is automatically added to all of the different launch options – Shabirmean May 09 '20 at 19:29
  • It's impossible. You only can add it one by one. – Steven-MSFT Jun 04 '20 at 07:35
  • I tried to add the argument for `agentlib` with `server=y` and it didn't override the default hard-coded one. It added a duplicate. I am afraid this option won't work. Can you help? – tarekahf Nov 24 '22 at 18:10
3

You should add vmArms at workspace level in setting.json file of VSCode

Open user setting: ctrl + shift + p

from popup then you can search "Open User Setting (JSON)"

Add this line below into your user setting json file

"java.debug.settings.vmArgs": "-Dappname=LocalTest ..."

press F5 (default) to run single java file or you can right click at the current file and choose "Run java"

SangLe
  • 128
  • 6