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 命令行离线核验
"""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 调试命令组装顺序。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
当前源码 SHA256:8eae29b96c0c4131cf67a17a9be41e9b481178e3e81488dc854fc046bf8be916
本地运行
解压后按 README 说明在对应本地环境运行脚本。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
ZIP SHA256:7d9849300b118c5713b2c0be58912ab82eddd5ee4ffe6b0bda6517162ed5d19c
常见错误与边界
- 实际端口、文档资源与应用配置都应和测试条件一致。
练习与解答
在 Eclipse Debug 配置的 `Arguments` 选项卡中,`Program arguments` 与 `VM arguments` 在启动 JVM 时分别起到什么作用?
展开参考解答
`VM arguments` 在主类之前传给 Java 虚拟机(用于配置堆大小、系统属性 `-D` 等运行时参数);`Program arguments` 在主类之后传入,作为字符串数组交给 `main(String[] args)` 处理。