You can use PSExec to execute processes as another user. The command line looks like this:
psexec.exe -u username -p password mybinary.exe
You can then use ProcessBuilder to create a team around this.
Edit: here is an example of how you can do this:
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; }
Then you can use it as follows:
startProcess("Bob", "Password", "Notepad.exe", "C:\\myfile.txt");
source share