try { String command; String os = System.getProperty("os.name").toLowerCase(); if (os.contains("linux") || os.contains("mac os x")) command = "shutdown -h now"; else if (os.contains("windows")) command = "shutdown /p"; else throw new Exception("Unsupported operating system."); Runtime.getRuntime().exec(command); System.exit(0); } catch (Exception ex) { JOptionPane.showMessageDialog(null, ex.getMessage()); }
For Linux and Mac OS X, the command for immediate shutdown is shutdown -h now. But in Windows, it is shutdown /p. The above java code checks the operating system. Then it assumes appropriate shutdown command and then executes it. This java code can shutdown a computer.