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

I am trying to read a .txt file in a line in my code, which I placed directly in the /src/ directory, when I run it in a test case or with a static void main, the path output is correct. However, when I launch the application with the Tomcat server, the root path of the application indicates where I load Eclipse - D:\eclipse\... , while the correct path should be D:\workspace\myproject\src\ . Then, of course, he will never be able to 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:

  • (it can be dumb) Where do we usually place a text file?
  • How can I change [ System.getProperty("user.dir"); ], so it will point to my project workspace?

Thanks!

Sharon


regarding your answer:

add the following arguments -Duser.home = 'Your Path', make sure you add -D at the beginning of your system variable. And you can put this variable in the “VM Arguments” field, presented on the “Parameters” tab, when you open the launch configuration when using the tomcat server.

I cannot find the place you are talking about. Is it in the Eclipse or Tomcat directory?

thanks

+6
source share
5 answers

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

+8
source

The short answer is that you cannot change the current working directory of the current Java application; see Changing the current working directory in Java?

Setting the user.dir property user.dir not work because it does not affect the current current directory that the OS uses when resolving path names for the application.

Installing -Duser.dir on the command line will also not work. Rather, you should:

  • if you run using script, cd in the appropriate directory before running the application,
  • if you start using ProcessBuilder , set the working directory using the directory(File) method or
  • If you are using the Eclipse launcher, set the "Working Directory" in the launcher configuration.

Finally, you are trying to make (IMO) a bad idea:

  • Some people write Tomcat and webapp configuration files on the assumption that the current Tomcat 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 will not want to return to a separate sandbox.

The best approach is to do something according to @ Eng.Fouad's answer.

+4
source

OK! I understood!

Yes,

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

is the way to go. and I am editing the tomcat argument in the Eclipse IDE. (Run → Run Config ... → Apache Tomcat → [Click] Tomcat vX Server → on the right screen, click "Argument" → "Workspace" section → "I will go to" Other "and set the actual working directory.)

I just realized that even I run tomcat. Not using the eclipse IDE, but Dos cmd and even deploying to the server, it still uses the working directory as D: \ Eclipse. But still change the working directory.

+2
source

try not to change the system properties; instead, add another parameter to the system properties.

For instance. add the following arguments -Duser.home='Your Path' to add -D at the beginning of your system variable. And you can put this variable in the “VM Arguments” field, presented on the “Parameters” tab, when you open the launch configuration when using the tomcat server.

And if you run the same from the main method, add vairable to the launch configuration, as this will support sactity in your code.

0
source

To read files from the root using the class path (eclipse automatically copies any file without java from src to classes):

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

Thus, you do not need to be confused with the current folder at all.

0
source

Source: https://habr.com/ru/post/890375/


All Articles