如何为 VSCode 的 Java 调试器提供启动选项参数

huangapple go评论99阅读模式
英文:

How do I provide launch option args to VSCode Java Debugger

问题

我正在阅读VSCode Java 调试器的 GitHub 页面上的自述文件。

页面上指定了一个名为“Options”的列表,可以提供这些选项。我不知道应该如何提供这些选项。我所知道的是我安装了这个扩展,现在当我按下 F5 键时,调试器就会启动。

在自述文件的更上方,它简要提到了一个名为 launch.json 的文件。这个文件在我的电脑上不存在。

我该如何提供 args 选项?具体地,我想要启用断言。

英文:

I am reading though the readme on the VSCode Java Debugger github page.

On the page is specifies a list "Options" that can be provided. I do not know how I am supposed to provide these options. All I know is I installed the extension, and now when I press F5 the debugger starts.

Further up in the readme, it briefly mentions a launch.json file. This file does not exist for me.

How can I provide args options. Specifically I want to enable assertions.

答案1

得分: 5

VSCode会在项目根目录的.vscode/文件夹中创建一个launch.json文件,当你在左侧面板点击“运行和调试”图标时:

打开launch.json文件。它可以包含一系列配置来启动你的应用程序。要启用断言,请将"vmArgs""args"用于传递给主类的参数)添加到已有的配置块中:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "java",
            "name": "Debug (Launch) with Assertions Enabled",
            "request": "launch",
            "mainClass": "com.myapp.Main",
            "vmArgs": "-ea" //可以是字符串或字符串数组
        }
    ]
}
英文:

VSCode will create a launch.json file in the .vscode/ dir at the root of your project when you click on the "Run and Debug" icon on left panel:

如何为 VSCode 的 Java 调试器提供启动选项参数

Open the launch.json file. It can contain a list of configurations to launch your app. To enable asserions, add "vmArgs" ("args" are for arguments to be passed to main class) to the already present config stub:

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

            {
                "type": "java",
                "name": "Debug (Launch) with Assertions Enabled",
                "request": "launch",
                "mainClass": "com.myapp.Main",
                "vmArgs" : "-ea" //it accepts a string or an array of strings
            }
        ]
}

答案2

得分: 2

launch.json文件是调试配置文件,您可以在“.vscode”文件夹下找到它。如果您还没有该文件,可以打开调试面板(Ctrl+Shift+D),然后点击“创建launch.json文件”来生成一个launch.json文件。

launch.json文件可以包含多个调试配置,位于“configurations”属性中,通过“{}, ”分隔。在调试面板(Ctrl+Shift+D)中,您可以选择不同的配置来应用于调试。

然后您可以很容易地理解如何将您提到的选项应用于配置launch.json文件。

例如,这是Java调试的默认配置:

{
    "type": "java",
    "name": "Debug (Launch) - Current File",
    "request": "launch",
    "mainClass": "${file}"
},

一些配置属于VSCode,一些由您已激活的扩展提供。

在这个例子中,“type”,“name”,“request”是由VSCode提供的,您可以参考[这个页面][1]了解它们的含义,并找到更多与VSCode相关的配置。

而您在自述文件中提到的配置是由“Java”扩展提供的,它们与扩展相关。例如,这个示例中的“mainClass”。

您可以参考[Java-Run and Debug][2]和[VSCode调试][3]页面,了解我所说内容的详细信息。


[1]: https://code.visualstudio.com/docs/editor/debugging#_launchjson-attributes
[2]: https://code.visualstudio.com/docs/java/java-debugging
[3]: https://code.visualstudio.com/docs/editor/debugging#_launch-configurations
英文:

The launch.json file was the configuration of debugging, you can find it under '.vscode' folder. If you have not it, you can open Deubg panel(Ctrl+Shift+D) and click 'create a launch.json file' to create a launch.json file.

The launch.json file can contain a multi debug configurations located in "configurations" property and was separated by '{},'. In the Debug panel(Ctrl+Shift+D) you can choose different configurations to apply to debug.

Then you can easily understand how to apply the Options you mentioned to configure the launch.json file.

Such as this is the default configuration of java debug:

        {
            "type": "java",
            "name": "Debug (Launch) - Current File",
            "request": "launch",
            "mainClass": "${file}"
        },

Some configurations belong to VSCode, some were provided by the extension which you have activated.

In this example, 'type', 'name', 'request' was provided by the VSCode, you can refer to this page to know the means of them and to find out more VSCode related configurations.

And what you have mentioned configuration in readme, was provided by the 'Java' extension, they are extension related. Such as 'mainClass' in this example.

You can refer to Java-Run and Debug and Debugging of VSCode page for the details of What I am talking about.

huangapple
  • 本文由 发表于 2020年9月3日 02:15:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63711388.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定