蓝色天空.知识 · 实践 · 创造
我的收藏
Eclipse 教程目录 35章
Eclipse / LEARN BY DOING

Eclipse Debug 配置

介绍如何通过 Run > Debug Configurations 为 Java 应用程序创建并保存调试启动配置,详解主类选择、程序参数、虚拟机参数及 Apply/Debug 启动流程。

学完这一章,你能够

  • 介绍如何通过 Run > Debug Configurations 为 Java 应用程序创建并保存调试启动配置,详解主类选择、程序参数、虚拟机参数及 Apply/Debug 启动流程。

打开 Debug Configurations 与基础配置项

Eclipse 的 Debug 配置(Debug Configuration)在结构上与普通运行配置(Run Configuration)高度一致,区别在于它会以调试模式(挂载 JDWP 调试通道)启动目标应用。打开入口为顶部菜单 `Run > Debug Configurations...`。

在对话框左侧的配置类型列表中选择 `Java Application` 并新建或选取具体条目,主面板(Main 选项卡)中的核心配置项包括:调试配置的名称(Name)、所属项目名称(Project)以及包含 `main` 入口的主类全限定名(Main class)。

配置 Arguments 参数并保存启动

切换到 `Arguments`(参数)选项卡后,可以按需分别填写两类启动参数:一是 `Program arguments`(程序参数,允许填写零个或多个,运行时按顺序传入 `main(String[] args)`);二是 `VM arguments`(虚拟机参数,同样支持零个或多个,用于传递给 JVM,例如堆内存限制 `-Xmx256m` 或系统属性 `-Denv=dev`)。

参数设置完成后,先点击 `Apply` 按钮持久化保存该调试配置,随后点击 `Debug` 按钮即可在调试模式下启动 Java 应用。本站离线实验脚本已对 `.launch` 调试配置解析、两类参数拆分与 JDWP 启动命令拼装完成校验。

运行 Eclipse Java Application Debug 配置、参数拆分与 JDWP 命令行离线核验

含完整源码 · 109 行
展开查看收起
python
"""Offline Eclipse Debug Configurations XML & arguments verifier for Ch20 (debug-configuration)."""
import json
import shlex
import xml.etree.ElementTree as ET

VERSION = "CPython 3.12.14 offline Eclipse Debug Configuration verifier (desktop SWT IDE absent)"


def check(name, summary, expected, actual):
    return {
        "name": name,
        "summary": summary,
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = []

    # Case 1: Parse Eclipse Java Application .launch debug configuration
    launch_xml = """
    <launchConfiguration type="org.eclipse.jdt.launching.localJavaApplication">
      <stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="OrderDemo"/>
      <stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="com.example.order.AppMain"/>
      <stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="--mode=debug input.csv"/>
      <stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-Xmx256m -Dapp.env=dev"/>
    </launchConfiguration>
    """
    root = ET.fromstring(launch_xml)
    attrs = {
        el.attrib["key"].split(".")[-1]: el.attrib["value"]
        for el in root.findall("stringAttribute")
    }
    config_name = "AppMain_Debug"

    cases.append(
        check(
            "debug_launch_config_xml",
            "Verify Java Application Debug Configuration stores name, project name, and main class",
            {
                "config_name": "AppMain_Debug",
                "launch_type": "org.eclipse.jdt.launching.localJavaApplication",
                "project": "OrderDemo",
                "main_type": "com.example.order.AppMain",
            },
            {
                "config_name": config_name,
                "launch_type": root.attrib["type"],
                "project": attrs["PROJECT_ATTR"],
                "main_type": attrs["MAIN_TYPE"],
            },
        )
    )

    # Case 2: Arguments tab supports zero or multiple program and VM arguments
    empty_prog = shlex.split("")
    multi_prog = shlex.split(attrs["PROGRAM_ARGUMENTS"])
    multi_vm = shlex.split(attrs["VM_ARGUMENTS"])

    cases.append(
        check(
            "program_and_vm_arguments_parse",
            "Verify Arguments tab handles zero or multiple Program arguments and VM arguments",
            {
                "empty_program_args_count": 0,
                "program_args": ["--mode=debug", "input.csv"],
                "vm_args": ["-Xmx256m", "-Dapp.env=dev"],
            },
            {
                "empty_program_args_count": len(empty_prog),
                "program_args": multi_prog,
                "vm_args": multi_vm,
            },
        )
    )

    # Case 3: Apply persists configuration and Debug builds JDWP launch command
    saved_store = {}
    saved_store[config_name] = dict(attrs, mode="debug")
    jdwp_flag = "-agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:5005"
    cmd = ["java", jdwp_flag, *multi_vm, "-cp", "OrderDemo/bin", attrs["MAIN_TYPE"], *multi_prog]

    cases.append(
        check(
            "apply_and_debug_command_assembly",
            "Verify Apply persists configuration and Debug launches JVM with JDWP before main class",
            {
                "persisted_mode": "debug",
                "vm_args_before_main": True,
                "program_args_after_main": True,
                "has_jdwp_agent": True,
            },
            {
                "persisted_mode": saved_store[config_name]["mode"],
                "vm_args_before_main": cmd.index("-Xmx256m") < cmd.index("com.example.order.AppMain"),
                "program_args_after_main": cmd.index("--mode=debug") > cmd.index("com.example.order.AppMain"),
                "has_jdwp_agent": any(arg.startswith("-agentlib:jdwp=") for arg in cmd),
            },
        )
    )

    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
验证 `.launch` XML 提取项目名与主类名、`Arguments` 选项卡处理零个或多个程序参数与 VM 参数,以及 `Apply` 保存与 JDWP 调试命令组装顺序。

查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包

本地运行

解压后按 README 说明在对应本地环境运行脚本。

下载本章实验包 (mbskycom202612a2259.zip) ↓ 查看运行说明 ↗

本章附件与实例包

下载课程关联工程包:Eclipse

课程关联工程包(登记关联35章) · 含当前章节或实例 · mbskycom202612a2259.zip

下载课程关联工程包:Eclipse

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2271.zip

常见错误与边界检查

  • 实际端口、文档资源与应用配置都应和测试条件一致。

技术规范与官方资料

练习与参考解答

查看参考解答

`VM arguments` 在主类之前传给 Java 虚拟机(用于配置堆大小、系统属性 `-D` 等运行时参数);`Program arguments` 在主类之后传入,作为字符串数组交给 `main(String[] args)` 处理。

SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习