I try to read a .txt file line by line in my code, which I placed it just right under the /src/
directory, when I run it with test case or with static void main, the path output is correct. However, when I run the application with Tomcat server, the app root path points to where I download my Eclipse - D:\eclipse\...
, while the correct path should be D:\workspace\myproject\src\
. Then, of course, it can never find the file.
Below is my code:
String workDir = System.getProperty("user.dir");
String file = "numFile.txt";
File myFile = new File(workDir + file);
String userPath = myFile.getPath();
So, my questions are:
- (this maybe dumb) Where should we normally place a text file?
- How can I change [
System.getProperty("user.dir");
], so it will point to my project workspace?
Thank you!
Sharon
regarding to your reply:
add following arguments -Duser.home='Your Path'
make sure you add -D
at the begining of your system variable. And this variable you can put in the VM Arguments box provided under arguments tab when you Open the Launch Configuration when using tomcat server.
I cannot find the place you are talking about. Is it in Eclipse or Tomcat directory?
thanks