aspose file tools*
The moose likes Servlets and the fly likes getting the value of .properties file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "getting the value of .properties file" Watch "getting the value of .properties file" New topic
Author

getting the value of .properties file

Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 757
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:


Jigar Naik


Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10201
    
166



Also you look at this thread.
[ January 01, 2007: Message edited by: Jaikiran Pai ]

[My Blog] [JavaRanch Journal]
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 757
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

Joined: Jul 20, 2005
Posts: 10201
    
166

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

Joined: Jul 20, 2005
Posts: 10201
    
166

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

Joined: Dec 12, 2006
Posts: 757
its still giving null pointer exception...

at java.util.Properties.load <unknown Source>
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 757
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

Joined: Jul 20, 2005
Posts: 10201
    
166

Is the inputstream null? Just before calling the load method, print the inputStream variable as follows:


What does it print?
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 757
input stream is null
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 757
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

Joined: Jul 20, 2005
Posts: 10201
    
166

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

Joined: Dec 12, 2006
Posts: 757
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

Joined: Dec 12, 2006
Posts: 757
uhhhh

look at this code... its still null !!!
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10201
    
166

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

Joined: Jul 20, 2005
Posts: 10201
    
166

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

Joined: Dec 12, 2006
Posts: 757
is it same as getRealPath ???
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 757
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

Joined: Jul 20, 2005
Posts: 10201
    
166

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

Joined: Dec 12, 2006
Posts: 757
ya thats what i'm trying...
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 757
i got

Input Stream : java.io.FileInputStream@db440a
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10201
    
166

Now that you are getting the inputstream, you will be able to read the value of the property as well.
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 757
ya we did it...

thanks a lot...
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10201
    
166

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getting the value of .properties file