简体   繁体   中英

System.getProperty(“mode”) returns “null”

We recently had to set up one of the tomcat servers from scratch. Tomcat version is 8.0.20. Deploying a war file, now System.getProperty("mode") returns "null" where it should return PREPROD.

It should read this "mode" from a mode.properties file which is located in the webapps directory. The two lines commented out show another part of code that does not work anymore on the new tomcat server. I replaced it with code that should work.

//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 is related to all properties in the Computer where the JVM is running... there is no mode key defined there, that is why you get null as value....

check out all the properties in the pc by doing:

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

and verify yourself, there is no mode key in that map...

You have to load mode.properties first, like this way

private Properties mode=null;
mode = new Properties();
mode.load(new FileInputStream(pathtoMODE));

String mode = mode.getProperty("mode");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM