You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Java中获取Windows/Linux系统Google Chrome版本以适配ChromeDriver

Java获取Chrome版本号解决方案

Java完全可以实现跨系统获取已安装Chrome版本的需求,网上所谓无法实现的说法不属实。

注册表代码无输出的修复方案

你当前的注册表查询代码没有输出,核心有两个问题:

  • Windows reg 命令不支持单引号包裹注册表路径,必须使用双引号
  • Runtime.exec()直接传入带特殊字符的整串命令会出现参数解析错误,同时未读取进程错误流容易导致进程阻塞无输出

修复后的Windows版本查询代码:

public static String getWindowsChromeVersion() throws IOException {
    // 用字符串数组传递命令和参数,避免解析错误
    Process process = Runtime.getRuntime().exec(new String[]{"cmd", "/c", "reg query \"HKEY_CURRENT_USER\\Software\\Google\\Chrome\\BLBeacon\" /v version"});
    // 同时读取输入流和错误流,避免进程阻塞
    BufferedReader inputReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
    BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
    
    String line;
    StringBuilder result = new StringBuilder();
    while ((line = inputReader.readLine()) != null) {
        result.append(line);
    }
    while ((line = errorReader.readLine()) != null) {
        // 可打印错误信息排查问题
        // System.err.println(line);
    }
    
    // 解析版本号,格式为 version REG_SZ x.x.x.x
    if (result.length() > 0) {
        String[] parts = result.toString().split("\\s+");
        for (int i = parts.length -1; i >=0; i--) {
            if (parts[i].matches("\\d+(\\.\\d+)+")) {
                return parts[i];
            }
        }
    }
    // 若当前用户注册表无结果,可尝试查询系统级注册表 HKEY_LOCAL_MACHINE 路径
    return null;
}

跨操作系统通用方案

可以同时兼容Windows、Linux、MacOS三端,不依赖固定安装路径:

  • Windows优先用注册表查询,查询失败再尝试调用where chrome命令查找安装路径后查询版本
  • Linux直接执行google-chrome --version或chromium-browser --version,默认Chrome会加入系统PATH
  • MacOS优先读取默认安装路径的版本,兜底用系统命令动态查找安装路径

完整跨端实现示例:

import org.apache.commons.lang3.SystemUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ChromeVersionDetector {
    private static final Pattern VERSION_PATTERN = Pattern.compile("\\d+(\\.\\d+){3}");

    public static String getChromeVersion() throws IOException {
        if (SystemUtils.IS_OS_WINDOWS) {
            return getWindowsChromeVersion();
        } else if (SystemUtils.IS_OS_LINUX) {
            return getLinuxChromeVersion();
        } else if (SystemUtils.IS_OS_MAC) {
            return getMacChromeVersion();
        }
        throw new UnsupportedOperationException("不支持的操作系统");
    }

    private static String getWindowsChromeVersion() throws IOException {
        // 先查当前用户注册表
        String version = queryRegForVersion("HKEY_CURRENT_USER\\Software\\Google\\Chrome\\BLBeacon");
        if (version != null) return version;
        // 查系统级注册表
        version = queryRegForVersion("HKEY_LOCAL_MACHINE\\Software\\Google\\Chrome\\BLBeacon");
        if (version != null) return version;
        // 注册表查不到尝试where命令找路径
        Process process = Runtime.getRuntime().exec(new String[]{"cmd", "/c", "where chrome"});
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String path = reader.readLine();
        if (path != null) {
            Process versionProcess = Runtime.getRuntime().exec(new String[]{path, "--version"});
            BufferedReader vReader = new BufferedReader(new InputStreamReader(versionProcess.getInputStream()));
            return extractVersion(vReader.readLine());
        }
        return null;
    }

    private static String queryRegForVersion(String regPath) throws IOException {
        Process process = Runtime.getRuntime().exec(new String[]{"cmd", "/c", "reg query \"" + regPath + "\" /v version"});
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            if (line.contains("REG_SZ")) {
                return extractVersion(line);
            }
        }
        return null;
    }

    private static String getLinuxChromeVersion() throws IOException {
        String[] commands = {"google-chrome --version", "chromium-browser --version"};
        for (String cmd : commands) {
            Process process = Runtime.getRuntime().exec(cmd.split(" "));
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = reader.readLine();
            if (line != null) {
                return extractVersion(line);
            }
        }
        return null;
    }

    private static String getMacChromeVersion() throws IOException {
        Process process = Runtime.getRuntime().exec(new String[]{"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", "--version"});
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = reader.readLine();
        if (line != null) {
            return extractVersion(line);
        }
        // 动态查找安装路径的兜底方案
        Process findProcess = Runtime.getRuntime().exec(new String[]{"mdfind", "kMDItemCFBundleIdentifier == 'com.google.Chrome'"});
        String appPath = new BufferedReader(new InputStreamReader(findProcess.getInputStream())).readLine();
        if (appPath != null) {
            Process vProcess = Runtime.getRuntime().exec(new String[]{appPath + "/Contents/MacOS/Google Chrome", "--version"});
            return extractVersion(new BufferedReader(new InputStreamReader(vProcess.getInputStream())).readLine());
        }
        return null;
    }

    private static String extractVersion(String content) {
        if (content == null) return null;
        Matcher matcher = VERSION_PATTERN.matcher(content);
        if (matcher.find()) {
            return matcher.group();
        }
        return null;
    }
}

内容的提问来源于stack exchange,提问作者mcool

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 08:15:03