Skip to content

Instantly share code, notes, and snippets.

@zambon
Last active August 29, 2015 13:56
Show Gist options
  • Save zambon/9140388 to your computer and use it in GitHub Desktop.
Save zambon/9140388 to your computer and use it in GitHub Desktop.
Finding an arbitrary user's home directory in Linux/UNIX
Usage:
javac TextExec.java
java TestExec
Output:
/home/username
import java.io.*;
public class TestExec {
public static void main(String[] args) {
try {
// Replace 'username' with the actual username
String[] cmd = {"/bin/sh", "-c", "echo ~username"};
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment