@robjbが私に最良の解決策を与えました。私の好みにはやや柔軟性はありませんが、私が問題に遭遇するまで、私はそれを訴えるでしょう。上記の例で
String shutdownCommand;
StringPP operatingSystem = new StringPP(System.getProperty("os.name"));
if (operatingSystem.containsIgnoreCase("linux") ||
operatingSystem.containsIgnoreCase("mac") ||
operatingSystem.containsIgnoreCase("unix"))
{
shutdownCommand = "sudo shutdown -h -t 30";
}
else if (operatingSystem.containsIgnoreCase("windows"))
{
shutdownCommand = "shutdown /s /d P:0:0 /t 30 /c \"Blue Husky Timer 2 is shutting down your system, as you requested. \n"
+ "You have 30 seconds to save and close programs\"";
}
else
{
throw new UnsupportedOperationException("Unsupported operating system.");
}
try
{
Runtime.getRuntime().exec(shutdownCommand);
}
catch (Throwable t)
{
Main.LOG.logThrowable(t);
}
System.exit(0);
、StringPP
は、上記#containsIgnoreCase
を使用するなどの方法でString
の能力を増強するカスタムクラスです。 Main.LOG
は私が作って使ったロギングユーティリティです。
見てください:http://stackoverflow.com/questions/25637/shutting-down-a-computer-using-java – Rob
このアプローチ(あなたの唯一の選択肢です)を取るとき、コマンドラインのように見えるデスクトップ上のURLをブラウズするのではなく、Runtime.exec()を実行してください。 –
"operatingSystem = System.getProperty(" os.name ")"を使用し、結果に基づいて "Runtime.exec()"を使用するポストは、彼の最高の賭けです。 – paulsm4