Link to home
Start Free TrialLog in
Avatar of Sathish David  Kumar N
Sathish David Kumar NFlag for India

asked on

How to get web application real path()

Hi ,

I have property file in src folder of my web application i want to load the file dynamically .
1.  ../ applicationName/src/DB.property
2.getServletContext()

i used this thing but not use full .

How can i get the realPath of the file?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

What kind of resource is it?
The problem is that when you run your application, it may run under any servlet runner (like tomcat).
Where your sources are located is independant of your running application.
So, the application in not aware of the "../app/src/.." folder.

For example, if you are programming under your c:\yourAppFolder\ and your are deploying your application in www.yourhostprovicer.com/yourApp, there is no way that your running application will know where is c:\yourAppFolder\ on YOUR computer.

If you want to do something like this, you will have to deploy your DB.property togheter and then, this file will no longuer reside under your .../src/... file structure.
>> I have property file in src folder of my web application
that's the place! after deployment this file will go to WEB-INF/classes. You don't need getServletContext() at all.
Avatar of astaplesnerd
astaplesnerd

It would be the simplest, for properties file 'x.properties' to use a ResourceBundle:
ResourceBundle b = ResourceBundle.getBundle("/x"); // x.properties is in WEB-INF/classes
String s = b.getString("foo");

Open in new window

Avatar of Sathish David  Kumar N

ASKER

i want to dynamically  the change the value of proprty file.

so for input,out stream i need to give the relative path or absolute path.
>> i want to dynamically  the change the value of proprty file.

How do plan to do that?
Have you tried:
.getServletContext().getRealPath("/")

This should give you the real path on the server like c:\...
...
once you have where your application is located on your local drive, add the specific location of your file (like /WEB-INF/classes/DB.property)
And then edit your file like if you were local.
 
@object its giving Null pointer exception 1
>> i want to dynamically  the change the value of proprty file.

How do you plan to do that?

inpS=new FileInputStream(getServletContext().getRealPath(X.propertyFileName).toString());
			  props.load(inpS);
			  inpS.close();
			  props.setProperty("DBUSERID", schemaName);
			  props.setProperty("DBPASSWORD",schemaName);
			  outS=new FileOutputStream(X.propertyFileName);
			  props.store(outS,null);
			  props.list(System.out);

Open in new window

Here i have a pblm to load the property file
What is the value of:
getServletContext().getRealPath(X.propertyFileName).toString()
>>props.store(outS,null);

In that case, you can't really use classpath/resource techniques. What is the answer to the last question of MikeQc?
Depending on your servlet container, you might have problems with this approach.  Many of the containers do not explode WAR/EAR files when you deploy them, at which point you will not be able to write to a properties file contained within.  Your best approach here is, assuming you have access to the server's file system, storing these properties in a directory outside of your WEB application and using either a Java or System environment variable to locate the path from within your application.  Include a default set of properties within the app that can be loaded and stored in the external file if it doesn't exist (i.e. at first deployment).

The algorithm for loading would be:

1.  Load default properties from classpath: /my.default.properties
2.  Get external path -- {externalPath}
3.  Check for existing properties file: {externalPath}/my.properties
4.  If file exists:
4a.  Load properties from {externalPath}/my.properties
4b.  Copy loaded external properties into the default property set, overwriting existing values
       with those configured on the server.
5.  Save loaded "default" properties (with overrides) to {externalPath}/my.properties

If you encapsulate this logic in a class, you can also add a setProperty method to the class that will update the loaded properties and re-save them to the external file.
its give null pointer exception....
So, either your X.propertyFileName is not good or it is not contained under your serveur.
What is the value of X.propertyFileName?
You need to check

X
and
X.propertyFileName

for null
>>>>In that case, you can't really use classpath/resource techniques.

if i dont give the class means it store the value in some other place.
thats i need realpath .

if outputStream doesnt need the real path mean i will go for getClas.getClassLoader(). .....
but outputStream need the correct  path !!
thats the property file Name . DBConnectionProps.Property.  x is class Name (for securty reason i dint mention class thats y i put as x)
X is  static class define the constant values . i am sure its having value
if i put the same code in main method and change the
x..propertyFileName as ../projectName/src/DBConnectionProps.Property
and same the inputFileStream  as only x..propertyFileName
its working fine but if  run in the web application i have pblm
I/o exception coming so that only i need real path
>>X is  static class define the constant values . i am sure its having value

Don't assume. Print the value of X and X.properttFoo in the web app and make sure they're valid
ya i did... its having value.
It's conceivable that the container's security manager won't resolve that path then. Print the value of System.getProperty("user.home") and see if a file under that path can be resolved
No that not give the file path its give c:\\ document and setting\\ sathish
>>No that not give the file path its give c:\\ document and setting\\ sathish

I think you missed the point. I know that's what it will give - that's what it's meant to give. The point is, if it CAN give that, then there's a BIG difference isn't there? The question is why (see my last comment)

If the container can resolve that path then why not put the properties file in there?

btw, there was no need to open a new question on the same subject really. Since you have, you might as well reframe it to find out what the difference is between the two
i changed like this
getServlet().getServletContext().getRealPath(XforcesConstants.propertyFileName).toString()

but file loading .....Its giving... error . how to change this

java.io.FileNotFoundException: C:\sathishkumar\Test\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\xforcesTool\src\DBConnectionProps.properties (The system cannot find the path specified)
That doesn't look like a hugely good place to put the properties file, but what does the following give you at the DOS prompt?


dir  C:\sathishkumar\Test\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\xforcesTool\src\DBConnectionProps.properties

Open in new window

The system cannot find the path specified.
but it give this path only ?
ASKER CERTIFIED SOLUTION
Avatar of Sathish David  Kumar N
Sathish David Kumar N
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
How are you going to write to that location using that?
getClass().getClassLoader().getResource().getPath()
That will return a String. Then what?
this.getClass().getClassLoader().getResourcesAsStream for inputStream

for outputStream parameter is getClass().getClassLoader().getResource().getPath()
OK. Have you actually tested that?
ya its working fine. why?
That's OK then
I chaged the code above i mention.