「java实现远程关机」java远程主机强迫关闭了
今天给各位分享java实现远程关机的知识,其中也会对java远程主机强迫关闭了进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、java语言的自动关机的代码
- 2、java怎么实现远程关闭计算机,远程唤醒的做好了,远程关机的不知道该怎么做
- 3、java如何实现一个到特定时间自动关机?
- 4、java实现关机
- 5、Java 如何实现 系统调用?
- 6、在windows xp系统的非管理员用户下如何用java程序定时自动关闭计算机机?
java语言的自动关机的代码
public class RuntimeTest {
public static void main(String[] args)
{
Runtime rt=Runtime.getRuntime();
try
{
rt.exec("shutdown.exe -s -t 40");
/*40的单位为秒,可以改成你想要的任何数字。
如果是想定时关机,可用这句:rt.exec("at 19:00 shutdown.exe -s");19:00可以换成你想要的时间*/
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
java怎么实现远程关闭计算机,远程唤醒的做好了,远程关机的不知道该怎么做
有管理员权限吧,那用shutdown命令就可以了
shutdown -m \\192.168.0.10 -f -s -t 0
shutdown /? 看看帮助信息。
C:\Windows\system32shutdown /?
Usage: shutdown [/i | /l | /s | /r | /g | /a | /p | /h | /e] [/f]
[/m \\computer][/t xxx][/d [p|u:]xx:yy [/c "comment"]]
No args Display help. This is the same as typing /?.
/? Display help. This is the same as not typing any options.
/i Display the graphical user interface (GUI).
This must be the first option.
/l Log off. This cannot be used with /m or /d options.
/s Shutdown the computer.
/r Shutdown and restart the computer.
/g Shutdown and restart the computer. After the system is
rebooted, restart any registered applications.
/a Abort a system shutdown.
This can only be used during the time-out period.
/p Turn off the local computer with no time-out or warning.
Can be used with /d and /f options.
/h Hibernate the local computer.
Can be used with the /f option.
/e Document the reason for an unexpected shutdown of a computer.
/m \\computer Specify the target computer.
/t xxx Set the time-out period before shutdown to xxx seconds.
The valid range is 0-315360000 (10 years), with a default of 30.
If the timeout period is greater than 0, the /f parameter is
implied.
/c "comment" Comment on the reason for the restart or shutdown.
Maximum of 512 characters allowed.
/f Force running applications to close without forewarning users.
The /f parameter is implied when a value greater than 0 is
specified for the /t parameter.
/d [p|u:]xx:yy Provide the reason for the restart or shutdown.
p indicates that the restart or shutdown is planned.
u indicates that the reason is user defined.
If neither p nor u is specified the restart or shutdown is
unplanned.
xx is the major reason number (positive integer less than 256).
yy is the minor reason number (positive integer less than 65536).
java如何实现一个到特定时间自动关机?
把时间设置,存到配置文件,Java程序去读取就可以实现;
参考:
public class shutdownSystem extends Thread{
//设置关机时与分
private static shutdownH=10;
private static shutdownM=10;
public void run(){
// 获取当关时与分
int thisH=Calendar .HOUR_OF_DAY;
int thisM=Calendar.MINUTE;
if(shutdownH==thisH shutdownM==thisM){
try {
//关机
java.lang.Runtime.getRuntime().exec( "shutdown -s ");
} catch (java.io.IOException e) {
e.printStackTrace();
}finally{
try{
//间隔一分钟检查一次,确保能检查到关机时间
this.sleep(60000);
}chatch(Exception ex){}
}
}
}
}
java实现关机
import java.util.*;
import java.io.*;
class Shutdown
{
public static void main(String[] args)
{
System.out.println("Shutdown in 10s");
try{
Runtime.getRuntime().exec("cmd /c Shutdown -t 10");
}catch(IOException e){}
}
}
上面这个程序实现你所说的定时10秒关机
至于定时开机...你告诉我怎么在关机的状态下执行我的程序,我就把开机的程序给你写出来.
Java 如何实现 系统调用?
通过 java.lang.Runtime 类可以方便的调用操作系统命令,或者一个可执行程序,下面的小例子我在windows和linux分别测试过,都通过。基本原理是,首先通过 Runtime.getRuntime() 返回与当前 Java 应用程序相关的运行时对象,然后调用run.exec(cmd) 另启一个进程来执行命令(cmd为要执行的命令)。
一、运行一个可执行程序
执行一个.exe的文件,或通过已安装的软件打开一个特定格式的文件,如word、chm或mp3等等。
1. 在window下可以直接执行一个.exe文件,如执行我在F盘下的tomcat安装文件,将命令写为:
String cmd = "F:\\apache-tomcat-6.0.20.exe";
2. 打开一个word文档。如果系统已经安装了office应用程序,就可以通过调用word的可执行程序来打开一个word文档:
String cmd = "D:\\Program Files\\Microsoft Office\\OFFICE11\\WINWORD.EXE F:\\test.doc";
当然这样写有点麻烦,我们想打开一个word文档时只要双击就可以了,用不着去找WINWORD.EXE。要是打开每一种格式的文件都得去找它的可执行程序,那可累死了,我们可以通过下面的代码,打开任意一个已知格式的文件(只要安装的打开这种文件格式的软件),相当于用鼠标双击一个文件的图标:
String cmd = "cmd.exe /c start F:\\test.doc";
我用C写了一个进程操作的小例子,放在 linux 下编译出的可执行文件叫“fork_wait”,然后把我的java文件编译成TestRunTime.class后扔到 linux 上,在控制台执行 java TestRunTime 命令,TestRunTime 和 fork_wait 程序均运行成功。
String cmd = "./fork_wait";
二、执行一个有标准输出的系统命令
通过调用进程的 getInputStream() 方法,可以获得执行命令的标准输出。在 windows 的cmd控制台窗口和 linux 控制台执行系统名利的格式是一样的,只是输入的命令不同而已。
如要执行windows控制台中ping命令,可写为:String cmd = "ping ";
执行linux的ls命令,可写为:String cmd = "ls -l";
如果要执行一个带参数的命令,可使用 String 数组形式,如:
String[] cmd=new String[3];
cmd[0]="/bin/sh";
cmd[1]="-c";
cmd[2]="ls -l ./";
下面是我写的小例子:
Java代码
package com.why.RunTime;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class TestRunTime {
public static void main(String[] args) {
//windows
// String cmd = "F:\\apache-tomcat-6.0.20.exe";
// String cmd = "D:\\Program Files\\Microsoft Office\\OFFICE11\\WINWORD.EXE F:\\test.doc";
// String cmd = "cmd.exe /c start F:\\test.doc";
String cmd = "ping ";
//linux
// String cmd = "./fork_wait";
// String cmd = "ls -l";
// String[] cmd=new String[3];
// cmd[0]="/bin/sh";
// cmd[1]="-c";
// cmd[2]="ls -l ./";
Runtime run = Runtime.getRuntime();//返回与当前 Java 应用程序相关的运行时对象
try {
Process p = run.exec(cmd);// 启动另一个进程来执行命令
BufferedInputStream in = new BufferedInputStream(p.getInputStream());
BufferedReader inBr = new BufferedReader(new InputStreamReader(in));
String lineStr;
while ((lineStr = inBr.readLine()) != null)
//获得命令执行后在控制台的输出信息
System.out.println(lineStr);// 打印输出信息
//检查命令是否执行失败。
if (p.waitFor() != 0) {
if (p.exitValue() == 1)//p.exitValue()==0表示正常结束,1:非正常结束
System.err.println("命令执行失败!");
}
inBr.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
在windows xp系统的非管理员用户下如何用java程序定时自动关闭计算机机?
Runtime.getRuntime().exec("shutdown -s -t 60");
-t后面设置时间就可以。
java实现远程关机的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java远程主机强迫关闭了、java实现远程关机的信息别忘了在本站进行查找喔。
发布于:2022-12-01,除非注明,否则均为
原创文章,转载请注明出处。