Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change System.getProperty("user.dir") to project workspace

Tags:

java

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

like image 443
Sharon Avatar asked Jun 13 '11 02:06

Sharon


People also ask

What does system getProperty user DIR do?

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.

What does system getProperty user Dir return in Linux?

getProperty("user. dir"); to get System property "user. dir" , which will return the user's working directory.

How do I set user folder in eclipse?

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.

What does User Dir mean?

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.

What is user DIR in Linux?

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'.


2 Answers

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

like image 169
Eng.Fouad Avatar answered Sep 28 '22 01:09

Eng.Fouad


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.

like image 43
Stephen C Avatar answered Sep 28 '22 01:09

Stephen C