Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change user.home system property

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?

like image 912
tekumara Avatar asked Sep 30 '09 23:09

tekumara


People also ask

How do I change my home directory in Java?

To set JAVA_HOME, do the following: Right click My Computer and select Properties. On the Advanced tab, select Environment Variables, and then edit JAVA_HOME to point to where the JDK software is located, for example, C:\Program Files\Java\jdk1. 6.0_02.

How do I set system properties in environment variables?

To get a specific system property you can use System. getProperty(String key) or System. getProperty(String key, String def) . Environment variables are set in the OS, e.g. in Linux export HOME=/Users/myusername or on Windows SET WINDIR=C:\Windows etc, and, unlike properties, may not be set at runtime.


2 Answers

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  
like image 73
ChssPly76 Avatar answered Oct 06 '22 06:10

ChssPly76


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
like image 37
joecracker Avatar answered Oct 06 '22 06:10

joecracker