1

in windows batch and and bash scripts there is %USERPROFILE% and ~, is there a command that works the same in java that will allow me to switch to the users home directory?

4
  • what do you mean by switch to? @Asaph's answer will give you the user's home directory but will not necessarily set a current working directory ala bash. Commented Sep 26, 2009 at 23:42
  • i don't necessarily mean switch, i was wanting to be able to save files there i just don't watch what i say. Commented Sep 26, 2009 at 23:44
  • @Lonnie: be careful. If you try to change the value the "user.home" property, it won't cause relative pathnames to resolve to a different place. The only thing it changes is the location of the empty abstract pathname; i.e. new File(""). Commented Sep 27, 2009 at 3:50
  • @Stephen C: in the comment immediately above yours, @Lonnie clarifies that he doesn't mean to switch directories (certainly not change the user's home directory). He just wants to determine the user's home directory so he can save files there. Commented Sep 27, 2009 at 4:04

2 Answers 2

1

System.getProperty("user.home"); gets you the user's home directory.

Sign up to request clarification or add additional context in comments.

4 Comments

how would i be able to save that as a variable? is it a string?
@Asaph. Changing the "user.home" property does not properly change the user's home directory. Read the javadoc for File.getAbsolutePath carefully.
@Stephen C. I never suggested changing "user.home". I only mentioned getProperty() in reference to "user.home". Furthermore, the documentation you mentioned located at java.sun.com/javase/6/docs/api/java/io/… makes mention of "user.dir" but not "user.home".
@Asaph: my mistake ... I really thought we were talking about "user.dir" rather than "user.home".
0

You get the current working directory, but not change it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.