Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getting the value of .properties file

 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created one mail application and used MVC atchitecture... and in that application i have created one httpHost.properties file

inside .properties file


and how do i access the value of that .properties file in to following

java class:
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Also you look at this thread.
[ January 01, 2007: Message edited by: Jaikiran Pai ]
 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i've tried this but its giving null pointer exception

Properties properties = new Properties();
URL url = ClassLoader.getSystemResource("smtpHost.properties");
properties.load(url.openStream());
String smtpHost = properties.getProperty("hostName");
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code is using the System Classloader to locate the properties file. And that might be the reason why the properties file is not being located and resulting the NullPointerException. Did you try the code in my earlier post?
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And also note that the path that you pass to the getResourceAsStream method is relative to your application. The code that i posted assumes that the file is at the root of your application.
 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its still giving null pointer exception...

at java.util.Properties.load <unknown Source>
 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JBoss Console :

16:06:35,273 ERROR [STDERR] java.lang.NullPointerException
16:06:35,273 ERROR [STDERR] at java.util.Properties$LineReader.readLine(Unkn
own Source)
16:06:35,273 ERROR [STDERR] at java.util.Properties.load(Unknown Source)
16:06:35,273 ERROR [STDERR] at com.tg.mail.mail.send_mail(mail.java:62)
16:06:35,273 ERROR [STDERR] at com.tg.mail.sendMail.doPost(sendMail.java:63)
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the inputstream null? Just before calling the load method, print the inputStream variable as follows:


What does it print?
 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
input stream is null
 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my smtpHost.properties file is in
SendMail (the name of project dir) ----> config ----> smtpHost.properties

and the structure of my SendMail.war file is

SendMail.war---> config---> smtpHost.properties
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jigar Naik:
my smtpHost.properties file is in
SendMail (the name of project dir) ----> config ----> smtpHost.properties

and the structure of my SendMail.war file is

SendMail.war---> config---> smtpHost.properties


As mentioned in one of my earlier posts, i had assumed that the properties file was at the root of the application. Now since the properties files is inside a folder named config, you will have to change the code to:



The rest of the code remains the same.
 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya i have tried the same... but its still giving null...

now i m putting the properties file at my desktop and giving it a full path... to c drive...
 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
uhhhh

look at this code... its still null !!!
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jigar Naik:
uhhhh

look at this code... its still null !!!


That wont work. The getResourceAsStream looks for the resource in the classpath.
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From where are you loading the properties file? I mean, is it a servlet? Also is that class part of the same SendMail.war application or is it part of some other application?
 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it same as getRealPath ???
 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from my java class... i have created one function send_mail in that i m writing all the code...

and .properties file is in my prooject only...
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just tried it out in one of my sample applications. There was a mistake in the code that i posted. Here's the code that works:



Note that the parameter to getResourceAsStream is config/smtpHost.properties. Earlier i had specified a / at the start of the path.

Try it out.
 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya thats what i'm trying...
 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i got

Input Stream : java.io.FileInputStream@db440a
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now that you are getting the inputstream, you will be able to read the value of the property as well.
 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya we did it...

thanks a lot...
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic