gpt4 book ai didi

java - Jenkins 抛出 java.lang.NoSuchFieldException : theCaseInsensitiveEnvironment

转载 作者:行者123 更新时间:2023-12-02 08:47:15 26 4
gpt4 key购买 nike

我试图通过单元测试设置环境变量,当我在本地计算机上运行代码时,它工作正常。

但是当我将它部署到 Jenkins 管道上时,它开始抛出异常。

异常:-

java.lang.NoSuchFieldException: theCaseInsensitiveEnvironment

代码片段:-

Class<?> processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment");
Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment");
theEnvironmentField.setAccessible(true);
Map<String, String> env = (Map<String, String>) theEnvironmentField.get(null);
env.clear();
env.putAll(newenv);
Field theCaseInsensitiveEnvironmentField = processEnvironmentClass.getDeclaredField("theCaseInsensitiveEnvironment");
theCaseInsensitiveEnvironmentField.setAccessible(true);
Map<String, String> cienv = (Map<String, String>) theCaseInsensitiveEnvironmentField.get(null);
cienv.clear();
cienv.putAll(newenv);

最佳答案

您正在解决错误的问题。你的业务代码不应该直接依赖于环境变量,中间应该有一个隔离层。将您的输入与服务代码分离,您将不再需要模拟环境变量。

关于java - Jenkins 抛出 java.lang.NoSuchFieldException : theCaseInsensitiveEnvironment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60994817/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com