I am creating a project using jsp/servlet in which I am trying to create java file and class file inside the project itself. But I am able to do this for only my system because the path I give their is like : C:\Users\MySystem\Desktop\Test\.. which works only for my system. What should I do so that if I have to run this project in another system I don't have to change path again and again.
Asked
Active
Viewed 653 times
-1
-
may be a duplicate of http://stackoverflow.com/questions/14209085/how-to-define-relative-path-in-java – Camille Douine Aug 07 '14 at 14:16
-
I think its something different or maybe I did not get it. – Mananpreet Singh Aug 07 '14 at 14:18
-
I hate seeing "please halp!11" in a question. You should definitely read this: http://www.catb.org/esr/faqs/smart-questions.html – Nikola Kolev Aug 07 '14 at 14:25
-
1In a `HttpServlet` you can use `this.getServletContext().getRealPath("")` to get the root directory of the deployed servlet. Not sure if it works for jsp, too. – das Keks Aug 07 '14 at 14:36
1 Answers
0
Well if it is maven project just put your resources files under src/main/resources
and you can read them using this lines.
String path = Thread.currentThread().getContextClassLoader()
.getResource("yourFileName").getPath();
System.out.println(path);
Or even this way you can do it.
String pathOfTheFile = getServletContext().getResource("yourFile").getPath();
and don't forget to put the file under web-content
or webapp
folder

SparkOn
- 8,806
- 4
- 29
- 34