为什么 user.dir 系统属性在 Java 中有效?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1234795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:45:44  来源:igfitidea点击:

Why is the user.dir system property working in Java?

javafilesystemschdir

提问by Geo

Almost every article I read told me that you can't have chdirin Java. The accepted answer to this questionsays you can't do it in Java.

我读过的几乎每篇文章都告诉我,在 Java 中不能有chdir这个问题的公认答案说你不能用 Java 来做。

However, here's some of the stuff I tried:

但是,这是我尝试过的一些东西:

geo@codebox:~$ java -version
java version "1.6.0_14"
Java(TM) SE Runtime Environment (build 1.6.0_14-b08)
Java HotSpot(TM) Client VM (build 14.0-b16, mixed mode, sharing)

Here's a test class I'm using:

这是我正在使用的测试类:

import java.io.*;

public class Ch {
    public static void main(String[] args) {
        System.out.println(new File(".").getAbsolutePath());
        System.setProperty("user.dir","/media");
        System.out.println(new File(".").getAbsolutePath());
    }
}
geo@codebox:~$ pwd
/home/geo
geo@codebox:~$ java Ch
/home/geo/.
/media/.

Please explain why this worked. Can I use this from now on and expect it to work the same way on all platforms?

请解释为什么这有效。我可以从现在开始使用它并期望它在所有平台上都以相同的方式工作吗?

回答by Jon Skeet

Just because new File(".")gives the desired answer doesn't mean it's doing what you want it to.

仅仅因为new File(".")给出了想要的答案并不意味着它正在做你想要的。

For example, try:

例如,尝试:

new FileOutputStream("foo.txt").close();

Where does that end up? On my Windows box, even though new File(".").getAbsolutePath()moves around based on user.dir, foo.txtis always created in the original working directory. It strikes me that setting user.dirsuch that new File(".")doesn'trefer to the current working directory is just asking for trouble.

最终会在哪里?在我的 Windows 机器上,即使new File(".").getAbsolutePath()基于 移动user.dirfoo.txt也总是在原始工作目录中创建。这让我感到设定user.dir,使得new File(".")参考当前的工作目录是自找麻烦。

回答by 01es

Quote:

引用:

The user.dir property is set at VM startup to be the working directory. You should not change this property or set it on the command-line. If you do, then you will see some inconsistent behaviour as there places in the implementation that assumes that the user.dir is the working directory and that it doesn't change during the lifetime of the VM.

user.dir 属性在 VM 启动时设置为工作目录。您不应更改此属性或在命令行上设置它。如果你这样做了,那么你会看到一些不一致的行为,因为在假设 user.dir 是工作目录并且在 VM 的生命周期内它不会更改的实现中有一些地方。

The discussion is here

讨论在这里

回答by Laurence Gonsalves

File.getAbsoluteFile() is just looking at the user.dir system property, which is a copy of the process's working directory at VM startup.

File.getAbsoluteFile() 只是查看 user.dir 系统属性,它是 VM 启动时进程工作目录的副本。

A better test might be to check that the process's working directory is actually changing. How you can do this varies by platform, but on Linux you can to something like:

更好的测试可能是检查进程的工作目录是否确实在更改。如何执行此操作因平台而异,但在 Linux 上,您可以执行以下操作:

$  ls -l /proc/18037/cwd
lrwxrwxrwx 1 laurence laurence 0 2009-08-05 11:16 /proc/18037/cwd -> /home/laurence/

where "18037" is the pid of the process in question. If you do this I believe you'll find that the process's working directory doesn't actually change when you update user.dir.

其中“18037”是相关进程的pid。如果你这样做,我相信你会发现当你更新 user.dir 时,进程的工作目录实际上并没有改变。