关于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上下载到 ...
随机推荐
- 【bzoj1040】骑士
[bzoj1040]骑士 题意 给定一个基环森林,求最大独立集. 分析 其实这是一道一年前做过的题. 只是今天在看bzoj1023的时候突然来了几许兴致,回过头来看一看. 如果对于一棵树的最大独立集, ...
- 核心Javascript学习
1. 引言: 1.1. 网页三要素: l HTML(内容) l CSS(外观) l Javascript(行为) 1.2. OOP的相关概念 1). 对象,方法和属性 l 对象就是指"事物 ...
- ie8 table td拆分宽度不适应问题
在table上加style="table-layout: fixed;"并在首行加一个高度为0且给定宽度的tr <table class="subtabledeta ...
- hdu 1598 find the most comfortable road(枚举+卡鲁斯卡尔最小生成树)
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- hdu----(1257)最少拦截系统(dp/LIS)
最少拦截系统 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- java学习之协调同步的线程
当一个线程使用的同步方法中用到某个变量,而此变量有需要其他线程修改后才能符合本线程的需要, 那么可以在同步方法中使用wait(),wait方法可以中断线程的执行,使本线程等待,暂时让出CPU的使用权, ...
- mysql jdbc连接
public class JDBCTest { public static void main(String[] args) { String sql = "SELECT * FROM us ...
- 解析 MACH_O 文件
现在做iOS开发的挺多,了解一下在苹果平台上程序运行的原理 解析 MACH_O 文件 这篇文章描述了如何解析 Mach-O 文件并稍微解释了一下它的格式.这不是一份权威指南,不过当你不知从何开始时,它 ...
- struts2 <s:iterator> status属性
struts2 <s:iterator> status属性 转载▼ iterator标签主要是用于迭代输出集合元素,如list set map 数组等,在使用标签的时候有三个属性值得我 ...
- Xcode中的几个常用文件路径
在iOS开发中有时候需要知道一些文件的路径,这里总结如下: 路径查找第一步如图: 1.模拟器的路径:/Applications/Xcode.app/Contents/Developer/Platfor ...