File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes File path in J2EE app Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "File path in J2EE app" Watch "File path in J2EE app" New topic
Author

File path in J2EE app

Olly Ontario
Greenhorn

Joined: Mar 17, 2008
Posts: 18
Hello there!

In a J2EE app I am reading a File using
FileReader fr = new FileReader("D:\\java\\examsys\\web\\Q.txt");

But this is the absolute path of the file.
Once I upload this app on the server the path will change.
I have tried all combinations for relative path like
"\\examsys\\web\\Q.txt"
"examsys\\web\\Q.txt"
".\\examsys\\web\\Q.txt"

it does not work
Is there a way out, Please...
This IDE is NetBeans with Tomcat included.

Rgds,
Olly
Brad Walton
Ranch Hand

Joined: Feb 16, 2008
Posts: 35
If this is with a Servlet, the way I've seen it done is with the context path. I did a google search on this recently, using 'java servlet file path' which turned up some useful results for me. Look here for example: http://forum.java.sun.com/thread.jspa?threadID=286165&messageID=1134946
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

You'll require to someting like :



e.g.



This will work if on the server your file is inside your application folder and the given path.


If you want real path, try something like



Hope that helps.
Olly Ontario
Greenhorn

Joined: Mar 17, 2008
Posts: 18
Hello!
Thanks for your clues!

I worked on them and found that using:

String strRelPath = request.getContextPath();

returns something like "\\examsys" so I had to concatenate it with + "\\web\\Q.txt"

to get the whole address "\\examsys\\web\\Q.txt"
But this did not work and I got a File not found exception.

Next I tried the

String strRealPath = getServletContext().getRealPath("");
this returned the actual path "D\\java\\examsys\\build\\web" and I just had to concatenate it with the file name + "Q.txt" and this worked.

But I still dont understand when I check in explorer the path of the file is "D\\java\\examsys\\web\\Q.txt"
but the real path while debugging returns as "D\\java\\examsys\\build\\web" and it still works.
I'm happy but still confused.
Any explanations?!

Rgds,
Olly
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Hello Olly,

As you say
real path while debugging returns "D\\java\\examsys\\build\\web"


and then you append the filename to it. So, this is how you get the real path to your file.

What happens is that when you say the relative path it return the context to your application folder. The use is since you may not know where is the application so you ask for the context and go ahead.

In your case you want a read operation and so you reuire the absolute path to the file inorder to actually get the file. So, you require the real path. This gets you to the web folder i.e the folder where all web executable content is stored.

Hope that solves the doubt.
Olly Ontario
Greenhorn

Joined: Mar 17, 2008
Posts: 18
Thanks Anubhav,

The thing that has got be confused is the different paths of the file:

in Explorer I am getting the path of the file as D:\java\examsys\web\Q.txt
while the real path returned in the webapp is D:\java\examsys\build\web\Q.txt

Dont know how the build folder got included in the path and the app still works.

Rgds,
Olly
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Well, the job of relative path is to return the context and the job of real path is to return the absolute path of the directory where your application is deployed.

As an example if your application name is MyApp and it is deployed in webapps directory of Tomcat say D:\Tomcat then,

Relative path (Context) will give --> /MyApp
and Real Path (absolute) will give --> D:\Tomcat\webapps\MyApp

Now, in your case as I see that you have kept the executabe content in "D:\java\examsys\web\" but, the compiled code will go to the build folder. Thus, the build folder gets reflected when you run the application.

I can't comment much unless you tell how the application is deployed and the other details.

Hope that helps.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File path in J2EE app
 
Similar Threads
Opening files from servlets
problem while reading in servlet..!
Properties files - directory structure
Reading File
Oracle 10g IAS xmlparserv2 problem