B4J Question Shutdown Computer

ykucuk

Well-Known Member
Licensed User
Longtime User
Hello,

i need restart & shutdown computer for all platforms (windows, mac, Linux)

i tried with Jshell but i cant success. Below code always return Exit code: 1

B4X:
Dim shl As Shell

shl.Initialize("shl", "shutdown.exe",  Array As String("-s -t 0"))

shl.WorkingDirectory = "C:\Windows\System32\"

shl.Run(10000) 'set a timeout of 10 seconds

I found that code on GitHub. is it possible convert this code to b4j

B4X:
public static void shutdown() throws RuntimeException, IOException {
   String shutdownCommand;
   String operatingSystem = System.getProperty("os.name");

   if ("Linux".equals(operatingSystem) || "Mac OS X".equals(operatingSystem)) {
       shutdownCommand = "shutdown -h now";
   }
   else if ("Windows".equals(operatingSystem)) {
       shutdownCommand = "shutdown.exe -s -t 0";
   }
   else {
       throw new RuntimeException("Unsupported operating system.");
   }

   Runtime.getRuntime().exec(shutdownCommand);
   System.exit(0);
}
 

NJDude

Expert
Licensed User
Longtime User
Try using PowerShell:

Restart:
B4X:
PowerShell.exe Restart-Computer
Shut Down:
B4X:
PowerShell.exe Stop-Computer
 
Upvote 0

OliverA

Expert
Licensed User
shl.Initialize("shl", "shutdown.exe", Array As String("-s -t 0"))
The parameters need to be broken out, else jshell and the command line only see one parameter
B4X:
Array As String(“-s”, “-t”, “0”)
Note: don’t copy paste my code sample since my phone is using different quote characters.
Note2: You need root for shutdown to work
Note3: corrected array. Each parameter and their values must be separate.
 
Last edited:
Upvote 0
Top