Daisetsu
Daisetsu

Reputation: 4976

Lock windows desktop from Java after timeout

I want my java app to lock the windows desktop after a specific timeout. I have a timer which works fine, but I can't seem to execute the command to lock the workstation.

javax.swing.Timer tim = new javax.swing.Timer(1000, new ActionListener() {
   public void actionPerformed(ActionEvent e) {
   System.out.println("CARD NOT PRESENT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
   // LOCK SCREEN
   Runtime rt = Runtime.getRuntime();
   Process pr = rt.exec("%windir%/System32/rundll32.exe user32.dll,LockWorkStation");
   }
});

Is there some mistake in this? Or maybe an easier way to do this?

Upvotes: 3

Views: 6338

Answers (3)

Steve S.
Steve S.

Reputation: 953

This is working as well (tested on Windows 7 x86):

final String path = System.getenv("windir") + File.separator + "System32" + File.separator + "rundll32.exe";
Process pr = rt.exec(path + " user32.dll,LockWorkStation");

Upvotes: 4

Roman Malieiev
Roman Malieiev

Reputation: 305

Try absolute location:

Runtime.getRuntime().exec("C:\\Windows\\System32\\rundll32.exe user32.dll,LockWorkStation");

Upvotes: 5

jayunit100
jayunit100

Reputation: 17648

I think there is a better way to test this :

1) put the command in a .bat file.

2) Run the bat file. does it work ?

3) If so , call the .bat file in your code.

4) Does it work ? If so, then you are done. I dont think there is any value in encoding windows specific code into runtime exec, just keep the bat file as a seperate file in your app .

Runtime.exec sometimes fails because the paths arent the same inside the JVM as they are in the native os.

Upvotes: 4

Related Questions