Change user.home system property

随声附和 提交于 2019-12-17 18:48:11

问题


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?


回答1:


Setting VM argument should work:

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

java test
/home/ChssPly76

java -Duser.home=overwritten test
overwritten 



回答2:


If you want to set user.home for all Java programs, you can use the special environment variable _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


来源:https://stackoverflow.com/questions/1501235/change-user-home-system-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!