Normally you use the statement
String userHome = System.getProperty( "user.home" );
to get the home directory of the user on any platform. See the method documentation for getProperty to see what else you can get.
There may be access problems you might want to avoid by using this workaround (Using a security policy file)
To find the home directory for user FOO on a UNIX-ish system, use ~FOO
. For the current user, use ~
.
Normally you use the statement
String userHome = System.getProperty( "user.home" );
to get the home directory of the user on any platform. See the method documentation for getProperty to see what else you can get.
There may be access problems you might want to avoid by using this workaround (Using a security policy file)
For an arbitrary user, as the webserver:
private String getUserHome(String userName) throws IOException{
return new BufferedReader(new InputStreamReader(Runtime.getRuntime().exec(new String[]{"sh", "-c", "echo ~" + userName}).getInputStream())).readLine();
}
I assume you want to find the home directory of a DIFFERENT user. Obviously getting the "user.home" property would be the easiest way to get the current user home directory.
To get an arbitrary user home directory, it takes a bit of finesse with the command line:
String[] command = {"/bin/sh", "-c", "echo ~root"}; //substitute desired username
Process outsideProcess = rt.exec(command);
outsideProcess.waitFor();
String tempResult;
StringBuilder sb = new StringBuilder();
while((tempResult = br.readLine()) != null) sb.append(tempResult);
br.close();
return sb.toString().trim();
Now technically, we should have a thread waiting on the stdout and stderr so the buffers don't fill up and lock up the process, but I'd sure hope the buffer could at least hold a single username. Also, you might want to check the result to see if it starts with ~root (or whatever username you used) just to make sure the user does exist and it evaluated correctly.
Hope that helps. Vote for this answer if it does as I'm new to contributing to SO and could use the points.
Can you parse /etc/passwd?
e.g.:
cat /etc/passwd | awk -F: '{printf "User %s Home %s\n", $1, $6}'
Try this on Java:
System.out.println("OS: " + System.getProperty("os.name") + ", USER DIRECTORY: " + System.getProperty("user.home"));
You can use the environment variable $HOME
for that.
Find a Java wrapper for getpwuid/getpwnam(3)
functions, they ask the system for user information by uid or by login name and you get back all info including the default home directory.
Try this on Java:
System.out.println("OS: " + System.getProperty("os.name") + ", USER DIRECTORY: " + System.getProperty("user.home"));
You can use the environment variable $HOME
for that.
Can you parse /etc/passwd?
e.g.:
cat /etc/passwd | awk -F: '{printf "User %s Home %s\n", $1, $6}'
Normally you use the statement
String userHome = System.getProperty( "user.home" );
to get the home directory of the user on any platform. See the method documentation for getProperty to see what else you can get.
There may be access problems you might want to avoid by using this workaround (Using a security policy file)
eval echo ~$SUDO_USER
might work.
If you want to find a specific user's home directory, I don't believe you can do it directly.
When I've needed to do this before from Java I had to write some JNI native code that wrapped the UNIX getpwXXX()
family of calls.
The userdir prefix (e.g., '/home' or '/export/home') could be a configuration item. Then the app can append the arbitrary user name to that path.
Caveat: This doesn't intelligently interact with the OS, so you'd be out of luck if it were a Windows system with userdirs on different drives, or on Unix with a home dir layout like /home/f/foo, /home/b/bar.
If your are trying to do this for a user name that you cannot hard code, it can be challenging. Sure echo ~rbronosky
would tell you the path to my home dir /home/rbronosky
, but what if rbronosky is in a variable? If you do name=rbronosky; echo ~$name
you get ~rbronosky
Here is a real world case and the solution:
You have a script that the user has to run via sudo. The script has to access the user's home folder. You can't reference ~/.ssh
or else it will expand to /root/.ssh
. Instead you do:
# Up near the top of your script add
export HOME=$(bash <<< "echo ~${SUDO_USER:-}")
# Then you can use $HOME like you would expect
cat rsa_key.pub >> $HOME/.ssh/authorized_keys
The beauty of it is that if the script is not run as sudo then $SUDO_USER is empty, so it's basically the same thing as doing "echo ~". It still works as you' expect.
If you use set -o nounset
, which you should be using, the variable reference ${SUDO_USER:-}
will default to blank, where $SUDO_USER
or ${SUDO_USER}
would give an error (because it is unset) if not run via sudo
.
Can you parse /etc/passwd?
e.g.:
cat /etc/passwd | awk -F: '{printf "User %s Home %s\n", $1, $6}'
If you want to find a specific user's home directory, I don't believe you can do it directly.
When I've needed to do this before from Java I had to write some JNI native code that wrapped the UNIX getpwXXX()
family of calls.
eval echo ~$SUDO_USER
might work.
For an arbitrary user, as the webserver:
private String getUserHome(String userName) throws IOException{
return new BufferedReader(new InputStreamReader(Runtime.getRuntime().exec(new String[]{"sh", "-c", "echo ~" + userName}).getInputStream())).readLine();
}
If you want to find a specific user's home directory, I don't believe you can do it directly.
When I've needed to do this before from Java I had to write some JNI native code that wrapped the UNIX getpwXXX()
family of calls.
To find the home directory for user FOO on a UNIX-ish system, use ~FOO
. For the current user, use ~
.
If your are trying to do this for a user name that you cannot hard code, it can be challenging. Sure echo ~rbronosky
would tell you the path to my home dir /home/rbronosky
, but what if rbronosky is in a variable? If you do name=rbronosky; echo ~$name
you get ~rbronosky
Here is a real world case and the solution:
You have a script that the user has to run via sudo. The script has to access the user's home folder. You can't reference ~/.ssh
or else it will expand to /root/.ssh
. Instead you do:
# Up near the top of your script add
export HOME=$(bash <<< "echo ~${SUDO_USER:-}")
# Then you can use $HOME like you would expect
cat rsa_key.pub >> $HOME/.ssh/authorized_keys
The beauty of it is that if the script is not run as sudo then $SUDO_USER is empty, so it's basically the same thing as doing "echo ~". It still works as you' expect.
If you use set -o nounset
, which you should be using, the variable reference ${SUDO_USER:-}
will default to blank, where $SUDO_USER
or ${SUDO_USER}
would give an error (because it is unset) if not run via sudo
.
The userdir prefix (e.g., '/home' or '/export/home') could be a configuration item. Then the app can append the arbitrary user name to that path.
Caveat: This doesn't intelligently interact with the OS, so you'd be out of luck if it were a Windows system with userdirs on different drives, or on Unix with a home dir layout like /home/f/foo, /home/b/bar.
You can use the environment variable $HOME
for that.
To find the home directory for user FOO on a UNIX-ish system, use ~FOO
. For the current user, use ~
.
Normally you use the statement
String userHome = System.getProperty( "user.home" );
to get the home directory of the user on any platform. See the method documentation for getProperty to see what else you can get.
There may be access problems you might want to avoid by using this workaround (Using a security policy file)
Can you parse /etc/passwd?
e.g.:
cat /etc/passwd | awk -F: '{printf "User %s Home %s\n", $1, $6}'
The userdir prefix (e.g., '/home' or '/export/home') could be a configuration item. Then the app can append the arbitrary user name to that path.
Caveat: This doesn't intelligently interact with the OS, so you'd be out of luck if it were a Windows system with userdirs on different drives, or on Unix with a home dir layout like /home/f/foo, /home/b/bar.
I assume you want to find the home directory of a DIFFERENT user. Obviously getting the "user.home" property would be the easiest way to get the current user home directory.
To get an arbitrary user home directory, it takes a bit of finesse with the command line:
String[] command = {"/bin/sh", "-c", "echo ~root"}; //substitute desired username
Process outsideProcess = rt.exec(command);
outsideProcess.waitFor();
String tempResult;
StringBuilder sb = new StringBuilder();
while((tempResult = br.readLine()) != null) sb.append(tempResult);
br.close();
return sb.toString().trim();
Now technically, we should have a thread waiting on the stdout and stderr so the buffers don't fill up and lock up the process, but I'd sure hope the buffer could at least hold a single username. Also, you might want to check the result to see if it starts with ~root (or whatever username you used) just to make sure the user does exist and it evaluated correctly.
Hope that helps. Vote for this answer if it does as I'm new to contributing to SO and could use the points.
Find a Java wrapper for getpwuid/getpwnam(3)
functions, they ask the system for user information by uid or by login name and you get back all info including the default home directory.
The userdir prefix (e.g., '/home' or '/export/home') could be a configuration item. Then the app can append the arbitrary user name to that path.
Caveat: This doesn't intelligently interact with the OS, so you'd be out of luck if it were a Windows system with userdirs on different drives, or on Unix with a home dir layout like /home/f/foo, /home/b/bar.
You can use the environment variable $HOME
for that.
Source: Stackoverflow.com