Java 更改 user.home 系统属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1501235/
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-08-12 13:32:28  来源:igfitidea点击:

Change user.home system property

javasystem-properties

提问by tekumara

How do I change the user.home system property from outside my java program, so that it thinks it's a different directory from D:\Documents and Settings\%USERNAME%? Via environment variables, or VM arguments?

如何从我的 java 程序外部更改 user.home 系统属性,以便它认为它与 D:\Documents and Settings\%USERNAME% 不同?通过环境变量还是 VM 参数?

采纳答案by ChssPly76

Setting VM argument should work:

设置 VM 参数应该有效:

java -Duser.home=<new_location> <your_program> 

Here's a test case:

这是一个测试用例:

public class test {
  public static void main(String[] args) {
    System.out.println(System.getProperty("user.home"));
  }
}

Tested with java 1.5.0_17 on Win XP and Linux

在 Win XP 和 Linux 上用 java 1.5.0_17 测试

java test
/home/ChssPly76

java -Duser.home=overwritten test
overwritten 

回答by joecracker

If you want to set user.homefor all Java programs, you can use the special environment variable _JAVA_OPTIONS.

如果要user.home为所有 Java 程序设置,可以使用特殊的环境变量_JAVA_OPTIONS

But note that a difficult to suppress warning message will be printed.

但请注意,将打印一条难以抑制的警告消息

$ export _JAVA_OPTIONS=-Duser.home=/some/new/dir
$ java test
Picked up _JAVA_OPTIONS: -Duser.home=/some/new/dir
/some/new/dir