You can use PSExec to execute processes as a different user. The command line looks like:
psexec.exe -u username -p password mybinary.exe
You can then use ProcessBuilder to build the command around this.
Edit: here is an example of how you can do it:
public int startProcess(String username, String password,
String executable, String... args) throws IOException {
final String psexec = "C:\\PsTools\\psexec.exe"; //psexec location
//Build the command line
List<String> command = new LinkedList<String>();
command.add(psexec);
if(username != null) {
command.add("-u");
command.add(username);
command.add("-p");
command.add(password);
}
command.add(executable);
command.addAll(Arrays.asList(args));
ProcessBuilder builder = new ProcessBuilder(command);
Process process = builder.start();
int returnCode;
try {
returnCode = process.waitFor();
} catch (InterruptedException e) {
returnCode = 1;
}
return returnCode;
}
You can then use it like this:
startProcess("Bob", "Password", "Notepad.exe", "C:\\myfile.txt");