关于process
http://docs.oracle.com/javase/1.5.0/docs/api/
The ProcessBuilder.start() and Runtime.exec methods create a native process and return an instance of a subclass of Process that can be used to control the process and obtain information about it. The class Process provides methods for performing input from the process, performing output to the process, waiting for the process to complete, checking the exit status of the process, and destroying (killing) the process.
The methods that create processes may not work well for special processes on certain native platforms, such as native windowing processes, daemon processes, Win16/DOS processes on Microsoft Windows, or shell scripts. The created subprocess does not have its own terminal or console. All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams (getOutputStream(), getInputStream(), getErrorStream()). The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.
The subprocess is not killed when there are no more references to the Process object, but rather the subprocess continues executing asynchronously.
There is no requirement that a process represented by a Process object execute asynchronously or concurrently with respect to the Java process that owns the Process object.
下面就开始举几个简单的示例:
(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();
- }
- }
- }
(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();
- }
- }
- }
执行上述命令可以打开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();
- }
- }
- }
- }
- }
(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){
- }
- }
- }
- }
- }
关于process的更多相关文章
- IIS启动失败,启动Windows Process Activation Service时,出现错误13:数据无效 ;HTTP 错误 401.2 - Unauthorized 由于身份验证头无效,您无权查看此页
因为修改过管理员账号的密码后重启服务器导致IIS无法启动,出现已下异常 1.解决:"启动Windows Process Activation Service时,出现错误13:数据无效&quo ...
- C#的Process类调用第三方插件实现PDF文件转SWF文件
在项目开发过程中,有时会需要用到调用第三方程序实现本系统的某一些功能,例如本文中需要使用到的swftools插件,那么如何在程序中使用这个插件,并且该插件是如何将PDF文件转化为SWF文件的呢?接下来 ...
- C# ShellExcute与Process
C#运行外部程序的两种方法 ShellExecute using System.Runtime.InteropServices; public enum ShowWindowCommands : in ...
- 【手记】调用Process.EnterDebugMode引发异常:并非所有引用的特权或组都分配给呼叫方
刚上线一个新版本,其中有台电脑打开软件就报[xx的类型初始值设定项引发异常](还好不是一大波电脑,新东西上线就怕哀鸿遍野),如图: 显然是该类型的静态构造函数中抛异常了(红线处就是类名),遂打开该类, ...
- C# - 多线程 之 Process与Thread与ThreadPool
Process 进程类, // 提供对本地和远程进程的访问,启动/停止本地系统进程 public class Process : Component { public int Id { get; } ...
- Java Business Process Management(业务流程管理) 初识环境搭建
一.简介 (一)什么是jbpm JBPM,全称是Java Business Process Management(业务流程管理),它是覆盖了业务流程管理.工作流.服务协作等领域的一个开源的.灵活的.易 ...
- Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details
thinkphp 在Apache上配置启用伪静态,重启Apache1 restart 竟然失败了,报错 Job for httpd.service failed because the control ...
- 更新过程 renewal process
一类随机过程.是描述元件或设备更新现象的一类随机过程.设对某元件的工作进行观测.假定元件的使用寿命是一随机变量,当元件发生故障时就进行修理或换上新的同类元件,而且元件的更新是即时的(修理或更换元件所需 ...
- Transaction (Process ID xxx) was deadlocked on lock
Transaction (Process ID 161) was deadlocked on lock | communication buffer resources with another pr ...
- NodeJS入门(五)—— process对象
process对象用于处理与当前进程相关的事情,它是一个全局对象,可以在任何地方直接访问到它而无需引入额外模块. 它是 EventEmitter 的一个实例. 本章的示例可以从我的Github上下载到 ...
随机推荐
- Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 排序、筛选、分页以及分组
Sorting, filtering, paging, and grouping 7 of 8 people found this helpful By Tom Dykstra The Contoso ...
- 数据库中间件mycat简单入门
当在项目中mysql数据库成为瓶颈的时候,我们一般会使用主从复制,分库分表的方式来提高数据库的响应速度,比如mysql主从复制,在没有数据库中间件的情况下,我们只能由开发工程师在程序中控制,这对于一个 ...
- jmeter笔记8
JMETER接口性能测试方案 JMETER简介 JMeter可以用于测试静态或者动态资源的性能(文件.Servlets.Perl脚本.java对象.数据库和查询.ftp服务器或者其 ...
- HTML5自学笔记[ 15 ]canvas绘图基础6
关于线条的一些属性: lineCap,这个属性表示的是线条两端的样式,值有butt(默认)/round/square. lineJoin,这个属性表示线条相交的方式,值有miter(默认)/bevel ...
- postgresql 索引
1.B-tree索引 create index idx_contacts_name on contacts(name); 2.数组索引 create index idx_contacts_phone ...
- Spring注释与简化配置
在Spring 2.5及以后的版本中,提供了注释和命名空间来简化Spring的配置.下面是一些常用配置分享. 1.@Autowired注释 以前给一个Bean配置属性时,Bean必须配置< ...
- win7开启硬盘AHCI
问题描述:装win7的时候没有在AHCI模式下安装,而是在IDE模式下安装的,后来安装完毕以后想更改成AHCI模式,可是更改以后启动电脑蓝屏并重启 解决方法: 如果是在IDE模式下安装的系统,由于在安 ...
- struts2 <s:iterator> status属性
struts2 <s:iterator> status属性 转载▼ iterator标签主要是用于迭代输出集合元素,如list set map 数组等,在使用标签的时候有三个属性值得我 ...
- 10 vi简介(重点)
1.为什么学习vi? vi很多系统都预装,如果我们的系统没有图像界面,可以使用vi vi是轻量级且执行快速的编辑器 2.vi的几种模式 命令模式.插入模式.底行模式 1) 命令行模式(command ...
- php安装出现的部分错误
在CentOS编译PHP5的时候有时会遇到以下的一些错误信息,基本上都可以通过yum安装相应的库来解决.以下是具体的一些解决办法: checking for BZip2 support… yes ch ...