@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
그 방법을 택할 때 (유일한 선택이 될 것입니다) Runtime.exec()'명령 줄과 같은 모양의 데스크탑에서 URL을 탐색하는 대신. –
"operatingSystem = System.getProperty ("os.name ")", "Runtime.exec()"결과를 기반으로하는 게시물은 절대적으로 최선의 선택입니다. – paulsm4