• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

File path in J2EE app

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 341
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 341
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 341
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic