This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
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.
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?!
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.
Joined: Mar 17, 2008
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.
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.