当前位置: 移动技术网 > IT编程>开发语言>Java > Java Process类的详解及实例代码

Java Process类的详解及实例代码

2019年07月22日  | 移动技术网IT编程  | 我要评论

java process类的详解

前言:

      今天用了下java.lang.process类,只是初步的学习,并没有深入实践,因为感觉它的用途并不是很大,偶尔才可能用上,如果要经常使用它的人可以自行参考jdk文档。

      对process类的简要说明:

     process类是一个抽象类,方法都是抽象的,它封装了一个进程,也就是一个可执行的程序  该类提供进程的输入、执行输出到进程、等待进程的完成和检查进程的退出状态及销毁进程的方法

     processbuilder.start()和runtime.exec方法创建一个本机进程并返回process子类的一个实例,该实例可以控制进程并获取相关的信息

     其它的概要请参考jdk文档

     下面就开始举几个简单的示例:

     (1)执行简单的dos命令,如打开一个记事本

package com.iwtxokhtd.other;  
 
import java.io.ioexception;  
 
public class processtest {  
 
  public static void main(string[] args) {  
    try {  
            process proc=runtime.getruntime().exec("notepad");  
    } catch (ioexception e) {  
      // todo auto-generated catch block  
      e.printstacktrace();  
    }  
 
  }  
 
} 

package com.iwtxokhtd.other; 
 
import java.io.ioexception; 
 
public class processtest { 
 
  public static void main(string[] args) { 
    try { 
            process proc=runtime.getruntime().exec("notepad"); 
    } catch (ioexception e) { 
      // todo auto-generated catch block 
      e.printstacktrace(); 
    } 
 
  } 
 
} 

 (2)使用它的其它构造方法执行相关的命令,如下例:

package com.iwtxokhtd.other;  
 
import java.io.ioexception;  
 
public class processtest {  
 
  public static void main(string[] args) {  
    try {  
        
      string exefullpathname="c:/program files/internet explorer/iexplore.exe";  
      string message="www.google.com";  
      string []cmd={exefullpathname,message};  
      process proc=runtime.getruntime().exec(cmd);  
    } catch (ioexception e) {  
      // todo auto-generated catch block  
      e.printstacktrace();  
    }  
 
  }  
 
} 


package com.iwtxokhtd.other; 
 
import java.io.ioexception; 
 
public class processtest { 
 
  public static void main(string[] args) { 
    try { 
       
      string exefullpathname="c:/program files/internet explorer/iexplore.exe"; 
      string message="www.google.com"; 
      string []cmd={exefullpathname,message}; 
      process proc=runtime.getruntime().exec(cmd); 
    } catch (ioexception e) { 
      // todo auto-generated catch block 
      e.printstacktrace(); 
    } 
 
  } 
 
} 

 执行上述命令可以打开google网站

(3)列出系统正在运行的所有进程信息

package com.iwtxokhtd.other;  
 
import java.io.bufferedreader;  
import java.io.ioexception;  
import java.io.inputstreamreader;  
 
public class listallprocesstest {  
 
  //列出所有的进程信息  
  public static void main(string[] args) {  
    bufferedreader br=null;  
    try {  
      process proc=runtime.getruntime().exec("tasklist");  
      br=new bufferedreader(new inputstreamreader(proc.getinputstream()));  
      @suppresswarnings("unused")  
      string line=null;  
      system.out.println("打印所有正在运行的进程信息");  
      while((line=br.readline())!=null){  
        system.out.println(br.readline());  
      }  
    } catch (ioexception e) {  
      e.printstacktrace();  
    }finally{  
      if(br!=null){  
        try {  
          br.close();  
        } catch (exception e) {  
          e.printstacktrace();  
        }  
      }  
    }  
      
 
  }  
 
} 


package com.iwtxokhtd.other; 
 
import java.io.bufferedreader; 
import java.io.ioexception; 
import java.io.inputstreamreader; 
 
public class listallprocesstest { 
 
  //列出所有的进程信息 
  public static void main(string[] args) { 
    bufferedreader br=null; 
    try { 
      process proc=runtime.getruntime().exec("tasklist"); 
      br=new bufferedreader(new inputstreamreader(proc.getinputstream())); 
      @suppresswarnings("unused") 
      string line=null; 
      system.out.println("打印所有正在运行的进程信息"); 
      while((line=br.readline())!=null){ 
        system.out.println(br.readline()); 
      } 
    } catch (ioexception e) { 
      e.printstacktrace(); 
    }finally{ 
      if(br!=null){ 
        try { 
          br.close(); 
        } catch (exception e) { 
          e.printstacktrace(); 
        } 
      } 
    } 
     
 
  } 
 
} 

(4)判断一个具体的进程是否正在运行,如下例:

package com.iwtxokhtd.other;  
import java.io.bufferedreader;  
import java.io.inputstreamreader;  
public class findprocessexetest  
{  
  public static void main(string []args){  
      
    if(findprocess("qq.exe")){  
      system.out.println("------判断指定的进程是否在运行------");  
      system.out.println("qq.exe该进程正在运行!");  
    }else{  
      system.out.println("------判断指定的进程是否在运行------");  
      system.out.println("qq.exe该进程没有在运行!");  
    }  
 
  }  
  public static boolean findprocess(string processname){  
    bufferedreader br=null;  
    try{  
       
      //下面这句是列出含有processname的进程图像名  
      process proc=runtime.getruntime().exec("tasklist /fi /"imagename eq "+processname+"/"");  
      br=new bufferedreader(new inputstreamreader(proc.getinputstream()));  
      string line=null;  
      while((line=br.readline())!=null){  
        //判断指定的进程是否在运行  
        if(line.contains(processname)){  
          return true;  
        }  
      }  
        
      return false;  
    }catch(exception e){  
      e.printstacktrace();  
      return false;  
    }finally{  
      if(br!=null){  
        try{  
          br.close();  
        }catch(exception ex){  
        }  
      }  
        
    }  
  }  
} 


package com.iwtxokhtd.other; 
import java.io.bufferedreader; 
import java.io.inputstreamreader; 
public class findprocessexetest 
{ 
  public static void main(string []args){ 
     
    if(findprocess("qq.exe")){ 
      system.out.println("------判断指定的进程是否在运行------"); 
      system.out.println("qq.exe该进程正在运行!"); 
    }else{ 
      system.out.println("------判断指定的进程是否在运行------"); 
      system.out.println("qq.exe该进程没有在运行!"); 
    } 
 
  } 
  public static boolean findprocess(string processname){ 
    bufferedreader br=null; 
    try{ 
       
      //下面这句是列出含有processname的进程图像名 
      process proc=runtime.getruntime().exec("tasklist /fi /"imagename eq "+processname+"/""); 
      br=new bufferedreader(new inputstreamreader(proc.getinputstream())); 
      string line=null; 
      while((line=br.readline())!=null){ 
        //判断指定的进程是否在运行 
        if(line.contains(processname)){ 
          return true; 
        } 
      } 
       
      return false; 
    }catch(exception e){ 
      e.printstacktrace(); 
      return false; 
    }finally{ 
      if(br!=null){ 
        try{ 
          br.close(); 
        }catch(exception ex){ 
        } 
      } 
       
    } 
  } 
} 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网