为什么 user.dir 系统属性在 Java 中起作用?

发布于 2024-07-30 10:07:24 字数 831 浏览 6 评论 0原文

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

但是,这是我尝试过的一些内容:

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)

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

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

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

Almost every article I read told me that you can't have chdir in Java. The accepted answer to this question says you can't do it in 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撕心裂肺的伤痛 2024-08-06 10:07:24

仅仅因为 new File(".") 给出了所需的答案并不意味着它正在执行您想要的操作。

例如,尝试:

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

最终会在哪里? 在我的 Windows 机器上,即使 new File(".").getAbsolutePath() 基于 user.dirfoo.txt 移动始终在原始工作目录中创建。 让我印象深刻的是,设置 user.dir 使得 new File(".") 引用当前工作目录只是在询问为了麻烦。

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

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.txt is always created in the original working directory. It strikes me that setting user.dir such that new File(".") doesn't refer to the current working directory is just asking for trouble.

人疚 2024-08-06 10:07:24

引用:

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

讨论位于此处

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.

The discussion is here

烟酒忠诚 2024-08-06 10:07:24

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

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

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

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

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

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:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文