Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you turn on and off a monitor from within a Java application?

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.

like image 241
Stephane Grenier Avatar asked Sep 07 '10 19:09

Stephane Grenier


3 Answers

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.

like image 154
Pablo Santa Cruz Avatar answered Nov 14 '22 22:11

Pablo Santa Cruz


While you can query the monitor configurations with Java, there is no way to turn the monitors on and off programatically without using JNI.

like image 37
Erick Robertson Avatar answered Nov 14 '22 23:11

Erick Robertson


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.

like image 37
meain Avatar answered Nov 14 '22 22:11

meain