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

后端 未结 5 1381
深忆病人
深忆病人 2020-12-06 18:54

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

相关标签:
5条回答
  • 2020-12-06 19:18

    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.

    0 讨论(0)
  • 2020-12-06 19:20

    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.

    0 讨论(0)
  • 2020-12-06 19:32

    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.

    0 讨论(0)
  • 2020-12-06 19:37

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

    0 讨论(0)
  • 2020-12-06 19:37

    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.

    0 讨论(0)
提交回复
热议问题