Logoff computer using java
Asked Answered
B

3

8

How to use java to log off windows. It could be using some windows own exe through Runtime(), or and other methods.It would be more efficient if you could provide some good alternative than using external programs like nircmd.exe .

When I use this code,

String shutdownCmd = "rundll32.exe shell32.dll,SHExitWindowsEx 0";
Process child = Runtime.getRuntime().exec(shutdownCmd);

I get

ERROR MSG

Brunson answered 23/9, 2013 at 16:44 Comment(1)
Well, running C:\Windows\System32\logoff.exe logs you off of Windows, so why don't you run that?Tailband
D
8

Do this (-l for logoff here, -r for restart, etc.):

String shutdownCmd = "shutdown -l";
Process child = Runtime.getRuntime().exec(shutdownCmd);

or (0 for logoff here):

String shutdownCmd = "rundll32.exe shell32.dll,SHExitWindowsEx 0";
Process child = Runtime.getRuntime().exec(shutdownCmd);
Divergent answered 23/9, 2013 at 16:48 Comment(1)
How to do hibernation?Brunson
I
2

You can also make a simple .bat file if it's just a file you want. Open notepad, type the code below, save as *all files, call it 'logoff.bat' or something and it should work.

shutdown -l
Introduction answered 23/9, 2013 at 16:55 Comment(1)
And you can also convert a .bat to and .exe if it's an .exe you want.Introduction
E
1

You could call the shutdown executable from the Runtime.getRuntime().exec(...) method. Check out this Q&A for details on that: How do I shutdown - restart - logoff Windows via a bat file?

Ettieettinger answered 23/9, 2013 at 16:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.