Home:ALL Converter>How to shutdown/reboot linux machine programatically without using commands(Runtime)

How to shutdown/reboot linux machine programatically without using commands(Runtime)

Ask Time:2016-11-01T06:44:05         Author:vignesh

Json Formatter

In Windows, it is possible to shutdown a machine through a java program by natively(JNA) calling a ExitWindowsEx function in Windows API. I am looking for something similar in linux. I know this can be done by executing commands but then I cannot rely on parsing the human-readable text.

Is IPC(DBus) the only way to do this or is it possible to load some library natively and the call the shutdown method natively. If there is a simpler way to do it please do let me know.

Using JNA or similar to shutdown and restart computer in linux and mac

I looked at this question earlier but it does not provide an answer as to how to do it programmatically.

Author:vignesh,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/40351655/how-to-shutdown-reboot-linux-machine-programatically-without-using-commandsrunt
Velkan :

Can't your program just invoke sh and execute shutdown command? Like that (needs root):\n\nsystem(\"shutdown\");\n\n\nOr if it's a desktop session:\n\nsystem(\"dbus-send --system --print-reply --dest=org.freedesktop.login1 /org/freedesktop/login1 \"org.freedesktop.login1.Manager.PowerOff\" boolean:true\");\n\n\nOr you can instead include a DBus connector library and do a proper IPC call.",
2016-11-03T07:03:15
yy