I have an application written in Java that needs to run under another windows user account each time. My question is - is it possible to automatically create and switch windows user accounts using Java? For example, suppose I create two local user accounts - one called "test12" and one called "test123" and I'm logged in to "test123" - I need to be able to switch to "test12" and then back to "test123" (or a different account) purely using Java. Is this possible? How?
I understand there are several question on SO that address this issue however I've only seen the question in C# or .net not in Java (For example, the following question Is it possible to Switch User Accounts programmatically? was asked in C# and the question Create Windows User programmatically c# .net (using PricinpalUser / CreateProfile) was asked in c#)
I am using windows 8 however I wanted the application to be able to run on Windows server 12 as well (if possible).
You should try executing native commands :
snippet for doing so :
try {
// Execute a command without arguments
String command = "dir";
Process child = Runtime.getRuntime().exec(command);
// Execute a command with an argument
command = "dir";
child = Runtime.getRuntime().exec(command);
} catch (IOException e) {
}
Windows command for running under specific user (might not answer your question but can help you):
Navigate to C:\WINDOWS\system32
Enter the following command: runas /user:*computer name\*account name explorer.exe
source :
http://lifehacker.com/290435/switch-user-accounts-from-the-command-prompt
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.