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:
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
getProperty() is used to obtain the system property. This system property is specified by the key which is the parameter for the method. To obtain the current working directory, the key used is user. dir.
getProperty("user. dir"); to get System property "user. dir" , which will return the user's working directory.
In Eclipse, the working directory defaults to the project directory. ( Project->Properties->Run/DebugSettings->Edit->Arguments tab, at the bottom of the page). Edit: Actually, the easiest way to get to it is Run->Open Run Dialog->Arguments tab. It's been a while since I used Eclipse.
This directory is named by the system property user. dir, and is typically the directory in which the Java virtual machine was invoked. In other words, user. dir is the working directory of the process that started the Java process at the time when it started the process.
The Linux home directory is a directory for a particular user of the system and consists of individual files. It is also referred to as the login directory. This is the first place that occurs after logging into a Linux system. It is automatically created as "/home" for each user in the directory'.
Try File myFile = new File(workDir, file);
The short answer is that you can't change a running application's current working directory in Java; see Changing the current working directory in Java?
Setting the user.dir
property won't work, because that doesn't affect the actual current directory that the OS uses when resolving pathnames for the application.
Setting the -Duser.dir
on the command line won't work either. Rather, you have to:
cd
to the relevant directory before running the application, ProcessBuilder
, set the working directory using the directory(File)
method, orFinally, what you are trying to do is (IMO) a bad idea:
$CATALINA_HOME/bin
. (This is wrong ... but your hack will break it.)A better approach is to do something along the lines of @Eng.Fouad's answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With