"""Offline Eclipse Run As, Alt+Shift+X J & Ctrl+F11 launch-history verifier for Ch16 (running-program)."""
import json
import re
VERSION = "CPython 3.12.14 offline Eclipse program-launch verifier (desktop Eclipse SWT GUI absent)"
MAIN_METHOD_RE = re.compile(
r"\bpublic\s+static\s+void\s+main\s*\(\s*String\s*(?:\[\s*\]|\.\.\.)\s*\w+\s*\)"
)
def check(name, summary, expected, actual):
return {
"name": name,
"summary": summary,
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def has_main_entry(java_source):
return bool(MAIN_METHOD_RE.search(java_source))
class EclipseLauncher:
def __init__(self):
self.configs = {}
self.history = []
def run_as_java_app(self, project, fqcn):
cfg_name = fqcn.rsplit(".", 1)[-1]
created = False
if cfg_name not in self.configs:
self.configs[cfg_name] = {"project": project, "main_type": fqcn}
created = True
self.history.append(cfg_name)
return cfg_name, created
def relaunch_last(self):
if not self.history:
return None
last = self.history[-1]
self.history.append(last)
return last
def main():
cases = []
# Case 1: Detect runnable Java classes containing public static void main
sources = {
"HelloMain.java": "public class HelloMain { public static void main(String[] args) { System.out.println(1); } }",
"VarargsMain.java": "public class VarargsMain { public static void main(String... args) {} }",
"UserModel.java": "public class UserModel { private String name; public String getName() { return name; } }",
}
runnable = sorted(name for name, code in sources.items() if has_main_entry(code))
cases.append(
check(
"main_method_entrypoint_detection",
"Identify Java classes with valid main() signatures eligible for Run As > Java Application",
["HelloMain.java", "VarargsMain.java"],
runnable,
)
)
# Case 2: Run As / Alt+Shift+X, J auto-creates Run Configuration on first run and reuses it thereafter
launcher = EclipseLauncher()
first_name, first_created = launcher.run_as_java_app("DemoProj", "com.mbsky.HelloMain")
second_name, second_created = launcher.run_as_java_app("DemoProj", "com.mbsky.HelloMain")
cases.append(
check(
"shortcut_and_auto_launch_config",
"Verify Run As / Alt+Shift+X, J auto-creates a Run Configuration once and reuses it on repeat invocation",
{
"config_name": "HelloMain",
"created_first_time": True,
"created_second_time": False,
"config_count": 1,
},
{
"config_name": first_name,
"created_first_time": first_created,
"created_second_time": second_created,
"config_count": len(launcher.configs),
},
)
)
# Case 3: Run -> Run / Ctrl+F11 relaunches the most recently launched Java application
launcher.run_as_java_app("DemoProj", "com.mbsky.ReportJob")
relaunched = launcher.relaunch_last()
cases.append(
check(
"ctrl_f11_last_launched_relaunch",
"Verify Run -> Run (Ctrl+F11) re-executes the most recently launched configuration",
{
"relaunched_config": "ReportJob",
"total_launches": 4,
"shortcut": "Ctrl + F11",
},
{
"relaunched_config": relaunched,
"total_launches": len(launcher.history),
"shortcut": "Ctrl + F11",
},
)
)
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/eclipse-running-program-lab/explore-running-program.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-running-program.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联35章) · 含当前章节或实例 · mbskycom202612a2259.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2287.zip