組み込みのJavaメソッドを使用してコンピューターをシャットダウンする方法はありますか?
9 に答える
コマンドラインからOSコマンドを実行する独自の関数を作成しますか?
例として。しかし、他の人が指摘しているように、これをどこで、なぜ使用したいかを知ってください。
public static void main(String arg[]) throws IOException{
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("shutdown -s -t 0");
System.exit(0);
}
クロスプラットフォームで機能する可能性のある別の例を次に示します。
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);
}
特定のshutdownコマンドには、異なるパスまたは管理者権限が必要な場合があります。
次に、ApacheCommonsLangの SystemUtilsを使用した例を示します。
public static boolean shutdown(int time) throws IOException {
String shutdownCommand = null, t = time == 0 ? "now" : String.valueOf(time);
if(SystemUtils.IS_OS_AIX)
shutdownCommand = "shutdown -Fh " + t;
else if(SystemUtils.IS_OS_FREE_BSD || SystemUtils.IS_OS_LINUX || SystemUtils.IS_OS_MAC|| SystemUtils.IS_OS_MAC_OSX || SystemUtils.IS_OS_NET_BSD || SystemUtils.IS_OS_OPEN_BSD || SystemUtils.IS_OS_UNIX)
shutdownCommand = "shutdown -h " + t;
else if(SystemUtils.IS_OS_HP_UX)
shutdownCommand = "shutdown -hy " + t;
else if(SystemUtils.IS_OS_IRIX)
shutdownCommand = "shutdown -y -g " + t;
else if(SystemUtils.IS_OS_SOLARIS || SystemUtils.IS_OS_SUN_OS)
shutdownCommand = "shutdown -y -i5 -g" + t;
else if(SystemUtils.IS_OS_WINDOWS)
shutdownCommand = "shutdown.exe /s /t " + t;
else
return false;
Runtime.getRuntime().exec(shutdownCommand);
return true;
}
この方法では、上記のどの回答よりもはるかに多くのオペレーティングシステムが考慮されます。また、プロパティをチェックするよりもはるかに見栄えが良く、信頼性が高くなりos.name
ます。
編集:遅延とすべてのバージョンのWindows(8/10を含む)をサポートします。
簡単な答えはノーです。これを行う唯一の方法は、アプリケーションがそれを行うために必要な特権を持っていると仮定して、コンピューターをシャットダウンさせるOS固有のコマンドを呼び出すことです。これは本質的に移植性がないため、アプリケーションが実行される場所を知るか、OSごとに異なるメソッドを使用して、使用するメソッドを検出する必要があります。
このプログラムを使用して、X 分でコンピューターをシャットダウンします。
public class Shutdown {
public static void main(String[] args) {
int minutes = Integer.valueOf(args[0]);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
ProcessBuilder processBuilder = new ProcessBuilder("shutdown",
"/s");
try {
processBuilder.start();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}, minutes * 60 * 1000);
System.out.println(" Shutting down in " + minutes + " minutes");
}
}
.equals を使用するよりも .startsWith を使用したほうがよい ...
String osName = System.getProperty("os.name");
if (osName.startsWith("Win")) {
shutdownCommand = "shutdown.exe -s -t 0";
} else if (osName.startsWith("Linux") || osName.startsWith("Mac")) {
shutdownCommand = "shutdown -h now";
} else {
System.err.println("Shutdown unsupported operating system ...");
//closeApp();
}
元気に働く
ラー。
JNIを使用して、C /C++で行う方法でそれを行うことができます。
簡単な単線
Runtime.getRuntime().exec("shutdown -s -t 0");
ただし、Windowsでのみ動作します
Windows Embedded では、デフォルトで cmd にシャットダウン コマンドがありません。このような場合、このコマンドを手動で追加するか、JNA (さらに Java が必要な場合) または JNI (C コードで特権を設定する方が簡単な場合) を使用して、win32 (user32.lib) から関数 ExitWindowsEx を使用する必要があります。