11

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:

  1. (this maybe dumb) Where should we normally place a text file?
  2. 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

Madbreaks
  • 19,094
  • 7
  • 58
  • 72
Sharon
  • 151
  • 1
  • 1
  • 5
  • possible duplicate of [Changing the current working directory in Java?](http://stackoverflow.com/questions/840190/changing-the-current-working-directory-in-java) – Stephen C Jun 13 '11 at 03:25
  • Thank you for your replies. 1. I have tried this.getClass().getClassLoader().getResourceAsStream("filename.txt"); => This returns null. Guess it just cannot find the file. 2. As @Eng.Fouad's answer: This is just a fix of my typo, yes I needed extra "/" File myFile = new File(workDir + "/" + file); However, it still will not work because I still have the problem that the "workDir" when running with Tomcat, will be pointed to D:\eclipse. Why would tomcat server think my workspace is under D:\eclipse? It must have been set somewhere. Help! I just cannot find the answer. Google for days. Thanks! –  Jun 13 '11 at 06:17

5 Answers5

10

Try File myFile = new File(workDir, file);

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
6

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:

  • if you are launching using a script, cd to the relevant directory before running the application,
  • if you are launching using a ProcessBuilder, set the working directory using the directory(File) method, or
  • if you are using an Eclipse launcher, set the "Working Directory" in the launch configuration.

Finally, what you are trying to do is (IMO) a bad idea:

  • Some folks write Tomcat and webapp config files on the assumption that Tomcat's current directory is the default location; e.g. $CATALINA_HOME/bin. (This is wrong ... but your hack will break it.)
  • When your application goes into production, you won't want to be referring back to some development sandbox.

A better approach is to do something along the lines of @Eng.Fouad's answer.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
4

OK! I got it!

Yes,

File myFile = new File(workDir + "/" + file);

is the way to go. and I edit the tomcat argument in Eclipse IDE. (Run -> Run Config... -> Apache Tomcat -> [Click] Tomcat vX Server -> at the right screen, click "Argument" -> Working directory section -> I change to Other and specify my actual working directory.)

It's just wierd that even I run tomcat Not by eclipse IDE, but Dos cmd and even deploy to server, it still apply the working directory as D:\Eclipse. But change the working directory worked anyway.

Sharon
  • 151
  • 1
  • 1
  • 5
  • 8
    Welcome to Stack Overflow! Since [Eng.Fouad gave this answer](http://stackoverflow.com/questions/6326228/how-to-change-system-getpropertyuser-dir-to-project-workspace/6326258#6326258) it would be polite to [accept it as correct](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235). It would also be polite to give up-votes to any answers which you found useful. Thanks. – sarnold Jun 13 '11 at 07:48
1

To read files from the root of the classpath use (eclipse automatically copies any non java file from src to classes):

this.getClass().getClassLoader().getResourceAsStream("filename.txt");

So you don't have to mess with the current folder at all.

Hendrik Brummermann
  • 8,242
  • 3
  • 31
  • 55
0

try not to change the system properties, instead, add one more parameter into the system properties.

For Eg. 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.

And if you are runing the same from some main method, then add the vairable in the run configuration, as this will maintain sactity in you code.

M.J.
  • 16,266
  • 28
  • 75
  • 97
  • 1
    1) 'user.home' is the wrong property. 2) it won't work anyway; see my answer. – Stephen C Jun 13 '11 at 04:55
  • @stephen : why wrong property? i can create my own system variables, when i donot need to use the existing system porperties, then i can create my own. – M.J. Jun 13 '11 at 06:14
  • Ah. I see what you are saying. (Your answer would be easier to read if it was properly punctuated!!!). Anyway you've picked a property that is already used by the JVM to mean something different; see the javadoc for `System.getProperties()`. Furthermore, you still have to code the webapp to respect the value of the property, because just setting the property will have no effect. – Stephen C Jun 13 '11 at 13:32