2

I am learning about the Runtime class in java, and am testing the use of command line instructions in it. I am trying to remotely shutdown a computer using Runtime.getRuntime().exec(...); When I run this, there are no errors, but the specified machine does not shut down. I don't know why.

Code:

import java.io.IOException;

class shutdownVirus {
    public static void main(String[] args) throws IOException {
        Runtime.getRuntime().exec("shutdown -m \\\\T12-LEOPARDIJ -s -t 10");
    }
}

Any help will be greatly appreciated. Thanks

Bl H
  • 133
  • 4
  • 12
  • 3
    Does the command work as expected when you run it directly from your command line (without Java in between)? – Thilo Jun 25 '12 at 04:16
  • The `exec` method returns a `Process` object. Wait some time after calling it and check for the `exitValue` of the returned `Process` object and/or check if there is something written to standard or error out by reading from the streams returned by `getInputStream()` and `getErrorStream()`. – siegi Jun 25 '12 at 04:22
  • No, it says "T12-LEOPARDIJ- Access denied.(5) – Bl H Jun 25 '12 at 04:24
  • how would you wait time after calling process object? and are you saying read from the streams from the process object? – Bl H Jun 25 '12 at 04:25
  • May be you would like to try [this][1]. [1]: http://stackoverflow.com/questions/25637/shutting-down-a-computer-using-java – Anuj Balan Jun 25 '12 at 04:40

2 Answers2

2

It probably does not work because the VM you are running the Java class with is started from a user that does not have shutdown rights. The process would inherit these access restrictions and therefore no be allowed to shut down.

Arguably thats a good thing..

Manfred Moser
  • 29,539
  • 13
  • 92
  • 123
  • By saying "arguably thats a good thing..." are you implying that I am attempting this for malicious means? Don't jump to conclusions... – Bl H Jun 25 '12 at 04:52
  • I am not. Malicious or not the JVM should not be able to run anything outside the OS security constraints. I am not implying that you are doing something malicious. I am implying that you should get the right access rights to do what you want to do. – Manfred Moser Jun 25 '12 at 16:05
1

Try

Runtime.getRuntime().exec("shutdown -s");

Edited part:

Use this.

shutdown /s /m <Computer_Name> To shutdown a remote PC

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75