gpt4 book ai didi

java - System.getProperty ("mode") 返回 "null"

转载 作者:行者123 更新时间:2023-11-28 22:07:36 27 4
gpt4 key购买 nike

我们最近不得不从头开始设置其中一台 tomcat 服务器。 Tomcat 版本为 8.0.20。部署一个 war 文件,现在 System.getProperty("mode") 在应该返回 PREPROD 的地方返回“null”。

它应该从位于 webapps 目录中的 mode.properties 文件中读取这个“模式”。注释掉的两行代码显示了另一部分代码在新的 tomcat 服务器上不再工作。我用应该可以工作的代码替换了它。

//String pathOfWebInf = sce.getServletContext().getRealPath("WEB-INF");
//String pathOfLocalhostFile = pathOfWebInf + File.separator + "classes"
// + File.separator;
String pathOfLocalhostFile = this.getClass().getResource("/").getPath();

String mode = System.getProperty("mode");
String fileName = "localhost-oracle.properties." + mode;

StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
encryptor.setPassword("xxx");

Properties dbProps = new EncryptableProperties(encryptor);
try
{
InputStream is = new FileInputStream(pathOfLocalhostFile + fileName);
dbProps.load(is);
} catch (Exception e)
{
throw new IOException("Could not read properties file " + pathOfLocalhostFile + fileName);
}

最佳答案

System.properties 与运行 JVM 的计算机中的所有属性相关...那里没有定义 mode 键,这就是你得到 null 的原因作为值(value)....

通过执行以下操作检查电脑中的所有属性:

final Properties props = System.getProperties();
props.list(System.out);

并验证自己,该 map 中没有mode键...

关于java - System.getProperty ("mode") 返回 "null",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41672903/

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