• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 970
  • Last Modified:

Reading a properties file under Spring

I have a Spring Webflow 2.0 application running under Tomcat 6.0 that needs to read a properties file. In a bean, I try to open and read the properties file, but no matter where I put the file, the JVM cannot find it (File Not Found exception). I ideally, we'd like the file to reside in the Tomcat config folder or elsewhere at that level. But I cannot find the file in either the application webapp folder or the Tomcat config folder.

How to do?
0
softechnics
Asked:
softechnics
  • 5
  • 5
  • 2
  • +1
1 Solution
 
Sathish David Kumar NArchitectCommented:
u can put ur property in class floder.
or in class path
0
 
Sathish David Kumar NArchitectCommented:
u must define the file name ur xml file(resorces bundel name)
0
 
Sathish David Kumar NArchitectCommented:
getMessageSourceAccessor().getMessage() u can get the property file value and locale also
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
softechnicsAuthor Commented:
>> u can put ur property in class floder or in class path

You're right, in theory, but it's not working. I do it with other webapps (non-Spring/Weblow) I've previously developed and never had a problem before. I believe there's something unique in Spring/Webflow (amongst other things) that causes it not to work.
0
 
objectsCommented:
how are you trying to load it?
you can either use tha classloader and getResource() methods
or getRealPath() to open it as a file
http://helpdesk.objects.com.au/java/how-to-get-the-path-of-a-file-in-a-web-application
0
 
Sathish David Kumar NArchitectCommented:
check your property file name and path (resources bundel name )in your xml file !
0
 
softechnicsAuthor Commented:
Objects,

I'm using the java.util.Properties class, load() and getProperty() methods.
0
 
softechnicsAuthor Commented:
>> check your property file name and path (resources bundel name )in your xml file !

What .xml file? It's not in web.xml and it's not in app-servlet.xml.
0
 
Sathish David Kumar NArchitectCommented:
where you definr ur resources bundel name?
0
 
softechnicsAuthor Commented:
In the java bean that opens the Properties file. There is no resources bundle name in web.xml or app-servlet.xml.
0
 
objectsCommented:
> I'm using the java.util.Properties class, load() and getProperty() methods.

what are you passing to load()?
If you're creating a FileInputStream/FileReader then you need to use getRealPath() to get the path
Or if you're loading the properties from your classes/lib folders then use getResourceAsStream()
0
 
rajkumar_pbCommented:
What OS are you using for Development?
BTW I am using Ubuntu and i used the below code to make it work

            Properties props = new Properties();        try {
//Absolute path
props.load(new FileInputStream("/home/user114/workspace/login/src
/pgsettings.properties"));

//Relative path - for linux you've to put it under home dir.props.load(new FileInputStream("/pgsettings.properties"));            } catch (FileNotFoundException e) {                  // TODO Auto-generated catch block                  e.printStackTrace();            } catch (IOException e) {                  // TODO Auto-generated catch block                  e.printStackTrace();            }        String user  = props.getProperty("pguser");        System.out.println("Domain is :"+user);
The above code is tested and works flawlessly for me.

For windows you need to put the properties file under WEB-INF folder.
0
 
softechnicsAuthor Commented:
Used getCanonicalPath(). There is no getRealPath(). But, it got me close enough to figure it out.

Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

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