How do you turn on and off a monitor from within a Java application?
In case you're wondering why, this is a kiosk style application where it would be great to turn off the monitors at night. Yes you can do it within the screensaver settings of the machine, but it would be great to do it programatically and avoid having to configure it on each machine.
Assuming you deploy your Java Applications on Windows, you can use this WIN32API functions:
// turn off monitor
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) 2);
// turn on monitor
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) -1);
Then you write a little C-JNI wrapper to functions that call the mentioned SendMessage
and use the little wrapper to turn off the monitor from Java.
While you can query the monitor configurations with Java, there is no way to turn the monitors on and off programatically without using JNI.
I have seen this exe named nircmd on the net. It is a freeware and can be used on by anyone.It has a built in function for doing this.
nircmd.exe monitor off
A detailed help is also found in the website. Just use it in your program and call it using
Runtime.getRuntime().exec(this.getClass().getResource("nircmd.exe").getPath());
or put it to the windows directory and run it from there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With