Spegnimento di un computer
-
09-06-2019 - |
Domanda
Esiste un modo per spegnere un computer utilizzando un metodo Java integrato?
Soluzione
Crea la tua funzione per eseguire un sistema operativo comando tramite la riga di comando?
Per il bene di un esempio.Ma sappi dove e perché vorresti usarlo come notano gli altri.
public static void main(String arg[]) throws IOException{
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("shutdown -s -t 0");
System.exit(0);
}
Altri suggerimenti
Ecco un altro esempio che potrebbe funzionare multipiattaforma:
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);
}
I comandi di spegnimento specifici potrebbero richiedere percorsi o privilegi amministrativi diversi.
Ecco un esempio utilizzando Apache Commons Lang 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;
}
Questo metodo prende in considerazione molti più sistemi operativi rispetto a qualsiasi delle risposte di cui sopra.Sembra anche molto più bello ed è più affidabile del controllo os.name
proprietà.
Modificare: Supporta il ritardo e tutte le versioni di Windows (incl.8/10).
La risposta rapida è no.L'unico modo per farlo è invocare i comandi specifici del sistema operativo che causeranno l'arresto del computer, presupponendo che l'applicazione disponga dei privilegi necessari per farlo.Questo è intrinsecamente non portatile, quindi dovresti sapere dove verrà eseguita la tua applicazione o avere metodi diversi per diversi sistemi operativi e rilevare quale utilizzare.
Utilizzo questo programma per spegnere il computer in X minuti.
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");
}
}
È meglio usare .startsWith piuttosto che usare .equals ...
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();
}
funziona bene
RA.
Puoi usare JNI per farlo in qualunque modo lo faresti con C/C++.
Su Windows Embedded per impostazione predefinita non è presente alcun comando di spegnimento in cmd.In tal caso è necessario aggiungere questo comando manualmente o utilizzare la funzione ExitWindowsEx da win32 (user32.lib) utilizzando JNA (se si desidera più Java) o JNI (se per voi è più semplice impostare i privilegi nel codice C).
semplice riga singola
Runtime.getRuntime().exec("shutdown -s -t 0");
ma funziona solo su Windows