1

I'm having issues in reading values from property file. In my project, there is a folder called "env". Inside that there are three folders named "default", "dev" and "staging". Those three folders contain three property files named "default.properties", "dev.properties" and "staging.properties". Inside all of those three property files, I have the below content:

# Emailing configurations
sender_email_address = osanda.nimalarathna@maxsoft.com
sender_email_password = 1qaz2wsx@
recipients_email_addresses = eranga.heshan@maxsoft.com
email_subject = MaxSoft IntelliAPI Email Test

enter image description here

Now what I am doing is reading them using java.

package com.maxsoft.ata.util;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Email {

    private static final String SENDER_EMAIL_ADDRESS = System.getenv("sender_email_address");
    private static final String SENDER_EMAIL_PASSWORD = System.getenv("sender_email_password");
    private static final String RECIPIENTS_EMAIL_ADDRESSES = System.getenv("recipients_email_addresses");
    private static final String EMAIL_SUBJECT = System.getenv("email_subject");

    public static void send(String messageBody) {

        System.out.println(SENDER_EMAIL_ADDRESS);

        /**
         * Email sending codes
         */

        }

    public static void main(String[] args) {
        send("test message");
    }
}

In the console, I am getting the output as null

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 3
    Would seem that someone needs to read [the documentation](https://docs.oracle.com/javase/10/docs/api/java/lang/System.html#getenv(java.lang.String)) and not make wild guesses as to what methods do. Hint: you're going to have to have a [`Properties.load`](https://docs.oracle.com/javase/10/docs/api/java/util/Properties.html#load(java.io.InputStream)) somewhere in your code. – Boris the Spider Mar 31 '18 at 07:54
  • Possible duplicate of [How to use Java property files?](https://stackoverflow.com/questions/1318347/how-to-use-java-property-files) – milbrandt Mar 31 '18 at 07:56

1 Answers1

0

Seems like you are couple of steps behind to achieve same.

  1. you have 3 different properties files as per env. so you need one mapper to map same.
  2. then load property file something like:

    InputStream is = null;
    try {
        this.prop = new Properties();
        is = this.getClass().getResourceAsStream("your env specific property file");
        prop.load(is);
    } catch (FileNotFoundException e) {
       //handle exception
    } catch (IOException e) {
         //handle exception
    }
    

It should work. (I did not test but I don't see any reason for failure).

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ben
  • 54
  • 4