蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

运行 Eclipse .classpath 条目分类、Add JARs vs Add External JARs、缺失库检测与输出映射离线核验

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-java-build-path.py
explore-java-build-path.py121 行
"""Offline Eclipse Java Build Path (.classpath) & JAR resolution verifier for Ch14 (java-build-path)."""
import json
import tempfile
import xml.etree.ElementTree as ET
from pathlib import Path

VERSION = "CPython 3.12.14 offline Eclipse Java-build-path verifier (desktop Eclipse SWT GUI absent)"


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


def main():
    cases = []

    # Case 1: Categorize .classpath XML entry kinds (src, con, lib, output)
    cp_xml = """<?xml version="1.0" encoding="UTF-8"?>
<classpath>
  <classpathentry kind="src" path="src/main/java"/>
  <classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
  <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
  <classpathentry kind="lib" path="lib/commons-lang3-3.12.0.jar"/>
  <classpathentry kind="lib" path="C:/shared-jars/ojdbc8.jar"/>
  <classpathentry kind="output" path="target/classes"/>
</classpath>
"""
    root = ET.fromstring(cp_xml)
    by_kind = {}
    for entry in root.findall("classpathentry"):
        by_kind.setdefault(entry.attrib["kind"], []).append(entry.attrib["path"])
    cases.append(
        check(
            "classpath_xml_entry_kinds",
            "Parse Eclipse .classpath XML and classify src, con, lib, and output entries",
            {
                "src_count": 2,
                "con_count": 1,
                "lib_count": 2,
                "default_output": "target/classes",
            },
            {
                "src_count": len(by_kind["src"]),
                "con_count": len(by_kind["con"]),
                "lib_count": len(by_kind["lib"]),
                "default_output": by_kind["output"][0],
            },
        )
    )

    # Case 2: Add JARs (workspace-relative) vs Add External JARs (absolute OS path)
    libs = by_kind["lib"]
    workspace_relative = [p for p in libs if not (p.startswith("/") or (len(p) > 1 and p[1] == ":"))]
    external_absolute = [p for p in libs if p.startswith("/") or (len(p) > 1 and p[1] == ":")]
    cases.append(
        check(
            "add_jars_vs_external_jars_paths",
            "Distinguish Add JARs (portable workspace-relative paths) from Add External JARs (absolute paths)",
            {
                "workspace_jars": ["lib/commons-lang3-3.12.0.jar"],
                "external_jars": ["C:/shared-jars/ojdbc8.jar"],
            },
            {
                "workspace_jars": workspace_relative,
                "external_jars": external_absolute,
            },
        )
    )

    # Case 3: Dependency resolution & missing-JAR build path error detection
    with tempfile.TemporaryDirectory() as tmp:
        proj = Path(tmp) / "OrderApp"
        (proj / "lib").mkdir(parents=True)
        (proj / "lib" / "commons-lang3-3.12.0.jar").write_bytes(b"PK\x05\x06" + b"\x00" * 18)
        declared_libs = ["lib/commons-lang3-3.12.0.jar", "lib/missing-driver.jar"]
        resolved = [p for p in declared_libs if (proj / p).is_file()]
        missing = [p for p in declared_libs if not (proj / p).is_file()]
    cases.append(
        check(
            "build_path_dependency_resolution",
            "Check declared .classpath JAR files against disk to separate resolved libraries from missing references",
            {
                "resolved": ["lib/commons-lang3-3.12.0.jar"],
                "missing": ["lib/missing-driver.jar"],
            },
            {
                "resolved": resolved,
                "missing": missing,
            },
        )
    )

    # Case 4: Source folder to custom/default output folder mapping
    default_out = root.find("classpathentry[@kind='output']").attrib["path"]
    src_out_map = {}
    for s in root.findall("classpathentry[@kind='src']"):
        src_out_map[s.attrib["path"]] = s.attrib.get("output", default_out)
    cases.append(
        check(
            "source_and_output_folder_mapping",
            "Verify per-source-folder custom output override vs default project output directory",
            {
                "src/main/java": "target/classes",
                "src/test/java": "target/test-classes",
            },
            src_out_map,
        )
    )

    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-java-build-path-lab/explore-java-build-path.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-java-build-path.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Eclipse

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

下载课程关联工程包:Eclipse

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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