码迷,mamicode.com
首页 > 编程语言 > 详细

Java 关机

时间:2015-11-22 21:37:33      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

RunTime.exec("Shutdown -s");
Runtime.getRuntime().exec("cmd /c Shutdown -t 10");

 1         Runtime run = Runtime.getRuntime();
 2         //run.exec("cmd /k shutdown -s -t 60");
 3         //run.exec("explorer");
 4         
 5         Process process = run.exec("cmd.exe /C E:\\ShutdownWindows.bat");
 6         try
 7         {
 8             if(process.waitFor() != 0)
 9             {
10                 //如果进程运行结果不为0,表示进程是错误退出的
11                 //获得进程实例的错误输出
12                 InputStream streamErr = process.getErrorStream();
13                 //do something
14                 
15                 System.out.println("Err : "+InputStream2String(streamErr));
16                 if (streamErr != null)
17                     streamErr.close();
18                 
19                 return;
20             }
21         } catch (InterruptedException e) {
22             // TODO Auto-generated catch block
23             e.printStackTrace();
24         } catch (Exception e) {
25             // TODO Auto-generated catch block
26             e.printStackTrace();
27         }
28         
29         InputStream streamIn = process.getInputStream();
30         String strRst;
31         try {
32             strRst = InputStream2String(streamIn);
33             System.out.println("Output : "+strRst);
34         } catch (Exception e) {
35             // TODO Auto-generated catch block
36             e.printStackTrace();
37         }
38         if (streamIn != null)
39             streamIn.close();

 

 1         try
 2         {
 3             String strsCmd = "\"C:\\Program Files (x86)\\Java\\jdk1.7.0_25\\bin\\java.exe\" -jar E:\\ShutdownWindows.jar";
 4             //String strsCmd = "cmd shutdown -s -t 30";
 5             //String strsCmd = "explorer";
 6             ProcessBuilder builder = new ProcessBuilder(strsCmd);
 7             
 8             //设置一个路径
 9 //            if (_strWorkDirectory != null)
10 //            {
11 //                builder.directory(new File(_strWorkDirectory));
12 //                builder.redirectErrorStream(true);
13 //            }
14             
15             Process process = builder.start();
16             //等待进程执行完毕
17             if(process.waitFor() != 0)
18             {
19                 //如果进程运行结果不为0,表示进程是错误退出的
20                 //获得进程实例的错误输出
21                 InputStream streamErr = process.getErrorStream();
22                 //do something
23                 
24                 System.out.println(InputStream2String(streamErr));
25                 if (streamErr != null)
26                     streamErr.close();
27             }
28             
29             InputStream streamIn = process.getInputStream();
30             String strRst = InputStream2String(streamIn);
31             System.out.println(strRst);
32             
33             if (streamIn != null)
34                 streamIn.close();
35         }
36         catch (Exception ex)
37         {
38             ex.printStackTrace();
39         }

 

上面两种方式,只在 java project 下  弄起来过,servlet中 都没成功。

尝试方式:

  1、servlet 直接 运行命令 "cmd /c shutdown -t 60" ==> 失败

  2、bat 中写 shutdown,servlet 调用 bat ==> 失败

  3、java project 中执行 shutdown,bat 调用 jar,servlet 调用 bat > 失败

 

 1 public String InputStream2String(InputStream _stream) throws Exception
 2     {
 3         String strRst = "";
 4         byte[] bytes = new byte[1024];
 5         while (_stream.read(bytes) != -1)
 6             strRst += new String(bytes);
 7         return strRst;
 8     }
 9 
10 InputStream2String

 

 

C

 

 

 

Java 关机

标签:

原文地址:http://www.cnblogs.com/codeskilla/p/4986722.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!