Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 502
  • Last Modified:

Accessing a properties file from within an ejb's method

Hi Experts,

I have a requirement to access a properties file that will be part of the EAR in an EJB within the same EAR.
Please let me know how I can achieve this requirement.

At present I have hard coded the code: The relevant part of the code is:

FileInputStream emailStream  = new FileInputStream("/usr/local/jnj_applications/cfg/blueprint/email.properties");
Properties props = new Properties();
props.load(emailStream);
 
InitialContext ctx = new InitialContext();
javax.mail.Session mailsession = (javax.mail.Session) ctx.lookup("MAIL_SERVER");
mailsession = mailsession.getInstance(props);

Please let me know ..........

Best Regards
0
M_SYED
Asked:
M_SYED
  • 2
  • 2
2 Solutions
 
phuocnhCommented:
Hi M_SYED!
FileInputStream emailStream  = <new A name of a class in your ear file>.class.getResourceAsStream("email.properties").
Now you just put email.properties into your classpath in your ear file. Take example put it into lib folder of ear file.
Phuoc
0
 
danielkullmannCommented:
Normally you have to give the full path of the file to Class.getResourceAsStream():
object.getClass.getResourceAsStream( "com.company.project.resources.email.properties" );
0
 
M_SYEDAuthor Commented:

Thanks for your comments.
I will try the options out and keep you updated.

Best Regards
0
 
M_SYEDAuthor Commented:
Hi,

I have changed the code, now it looks like:

InputStream emailStream  = this.getClass().getResourceAsStream ("email.properties");
Properties props = new Properties();
props.load(emailStream);
 
InitialContext ctx = new InitialContext();
javax.mail.Session mailsession = (javax.mail.Session) ctx.lookup("MAIL_SERVER");
mailsession = mailsession.getInstance(props);

Here the file email.properties resides in the same location as class in which I am calling it. But I am getting a Nul Pointer Exception in the line
props.load(emailStream);

Seems that the InputStream is null.

Is the problem that this.getClass() does not work inside an EJB?

Please let me know.

0
 
phuocnhCommented:
No, you put the email.properties in the classpath not same location as class (this).
In the ear file you can put it in the lib or class sub folder.
Phuoc
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now