Class JarConflictDetector

java.lang.Object
cloud.opencode.base.classloader.conflict.JarConflictDetector

public final class JarConflictDetector extends Object
JAR version conflict detection utility JAR 版本冲突检测工具

Scans JAR files to detect duplicate class definitions across multiple JARs. This is useful for diagnosing classpath conflicts that can cause NoSuchMethodError, ClassCastException, and other runtime issues.

扫描 JAR 文件以检测多个 JAR 中的重复类定义。这对于诊断可能导致 NoSuchMethodErrorClassCastException 和其他运行时问题的类路径冲突非常有用。

Features | 主要功能:

  • Detect duplicate classes across JARs - 检测 JAR 间的重复类
  • Scan directories for JAR files - 扫描目录中的 JAR 文件
  • Extract version info from MANIFEST.MF - 从 MANIFEST.MF 提取版本信息
  • Graceful handling of corrupt JARs - 优雅处理损坏的 JAR

Usage Examples | 使用示例:

// Detect conflicts between specific JARs
ConflictReport report = JarConflictDetector.detect(
    Path.of("/libs/guava-31.jar"),
    Path.of("/libs/guava-32.jar")
);

// Detect conflicts in a directory
ConflictReport report = JarConflictDetector.detectInDirectory(Path.of("/libs"));

if (report.hasConflicts()) {
    System.out.println(report.summary());
}

Security | 安全性:

  • Thread-safe: Yes (stateless utility) - 线程安全: 是(无状态工具)
  • Path traversal protection for directory scanning - 目录扫描的路径遍历保护
Since:
JDK 25, opencode-base-classloader V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • detect

      public static ConflictReport detect(Path... jarPaths)
      Detect class conflicts across the specified JAR files 检测指定 JAR 文件之间的类冲突
      Parameters:
      jarPaths - paths to JAR files to scan | 要扫描的 JAR 文件路径
      Returns:
      conflict report | 冲突报告
      Throws:
      NullPointerException - if jarPaths is null or contains null | 如果 jarPaths 为 null 或包含 null
    • detect

      public static ConflictReport detect(Collection<Path> jarPaths)
      Detect class conflicts across the specified JAR files 检测指定 JAR 文件之间的类冲突
      Parameters:
      jarPaths - collection of paths to JAR files to scan | 要扫描的 JAR 文件路径集合
      Returns:
      conflict report | 冲突报告
      Throws:
      NullPointerException - if jarPaths is null or contains null | 如果 jarPaths 为 null 或包含 null
    • detectInDirectory

      public static ConflictReport detectInDirectory(Path directory)
      Detect class conflicts among all JAR files in a directory 检测目录中所有 JAR 文件之间的类冲突
      Parameters:
      directory - the directory to scan | 要扫描的目录
      Returns:
      conflict report | 冲突报告
      Throws:
      NullPointerException - if directory is null | 如果 directory 为 null
    • detectInDirectory

      public static ConflictReport detectInDirectory(Path directory, String globPattern)
      Detect class conflicts among JAR files matching a glob pattern in a directory 检测目录中匹配 glob 模式的 JAR 文件之间的类冲突
      Parameters:
      directory - the directory to scan | 要扫描的目录
      globPattern - glob pattern for filtering (e.g. "*.jar") | 用于过滤的 glob 模式
      Returns:
      conflict report | 冲突报告
      Throws:
      NullPointerException - if directory or globPattern is null | 如果 directory 或 globPattern 为 null