java获取程序运行时RT打开qq
2015-01-01 20:55
375 查看
代码:
import java.io.IOException;
public class TestRT {
/**
* 使用Runtime对象的exec方法,调用外部exe文件。
*/
public static void main(String[] args) {
Runtime rt = Runtime.getRuntime();
try {
rt.exec("mspaint.exe"); //打开画图程序
rt.exec("C:\\Program Files (x86)\\Tencent\\QQ\\QQProtect\\Bin\\QQProtect.exe"); //"C:\\Program Files
//(x86)\\Tencent\\QQ\\QQProtect\\Bin\\QQProtect.exe"
} catch (IOException e) {
e.printStackTrace();
}
}
}
说明:
每个java运行程序都有一个Runtime运行实例,作用是使得应用程序与当前的运行环境对接。通过getRuntime()获取实例。
引用程序不能创建该实例(环境嘛,只能获取)
1. static Runtime getRuntime()
返回与当前 Java 应用程序相关的运行时对象。
2. Process exec(String command)
在单独的进程中执行指定的字符串命令。
代码2:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JOptionPane;
public class TestRT {
public final static int END_MARK = 0;
/**
* 使用Runtime对象的exec方法,运行cmd命令。
*/
public static void main(String[] args) {
Runtime rt = Runtime.getRuntime();
try {
Process pr = rt.exec("ping www.hao123.com "); //运行cmd命令
BufferedReader br = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String s = br.readLine();
String temp = "" ;
while(null != s ){
if(!"".equals(s.trim())) temp = s;
System.out.println(s);
s = br.readLine();
}
br.close();
//导致当前线程等待,如果必要,一直要等到由该 Process 对象表示的进程已经终止。
pr.waitFor();
//此 Process 对象表示的子进程的出口值。根据惯例,值 0 表示正常终止。
if (END_MARK == pr.exitValue()) {
JOptionPane.showMessageDialog(null, temp );
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
解释: Process.waitFor(),该方法会导致当前process堵塞,直到process线程推出,然后继续运行waitFor()后面的方法。
exp:
result:
import java.io.IOException;
public class TestRT {
/**
* 使用Runtime对象的exec方法,调用外部exe文件。
*/
public static void main(String[] args) {
Runtime rt = Runtime.getRuntime();
try {
rt.exec("mspaint.exe"); //打开画图程序
rt.exec("C:\\Program Files (x86)\\Tencent\\QQ\\QQProtect\\Bin\\QQProtect.exe"); //"C:\\Program Files
//(x86)\\Tencent\\QQ\\QQProtect\\Bin\\QQProtect.exe"
} catch (IOException e) {
e.printStackTrace();
}
}
}
说明:
每个java运行程序都有一个Runtime运行实例,作用是使得应用程序与当前的运行环境对接。通过getRuntime()获取实例。
引用程序不能创建该实例(环境嘛,只能获取)
1. static Runtime getRuntime()
返回与当前 Java 应用程序相关的运行时对象。
2. Process exec(String command)
在单独的进程中执行指定的字符串命令。
代码2:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JOptionPane;
public class TestRT {
public final static int END_MARK = 0;
/**
* 使用Runtime对象的exec方法,运行cmd命令。
*/
public static void main(String[] args) {
Runtime rt = Runtime.getRuntime();
try {
Process pr = rt.exec("ping www.hao123.com "); //运行cmd命令
BufferedReader br = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String s = br.readLine();
String temp = "" ;
while(null != s ){
if(!"".equals(s.trim())) temp = s;
System.out.println(s);
s = br.readLine();
}
br.close();
//导致当前线程等待,如果必要,一直要等到由该 Process 对象表示的进程已经终止。
pr.waitFor();
//此 Process 对象表示的子进程的出口值。根据惯例,值 0 表示正常终止。
if (END_MARK == pr.exitValue()) {
JOptionPane.showMessageDialog(null, temp );
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
解释: Process.waitFor(),该方法会导致当前process堵塞,直到process线程推出,然后继续运行waitFor()后面的方法。
exp:
public class ProcessDemo { public static void main(String[] args) { try { // create a new process System.out.println("Creating Process..."); Process p = Runtime.getRuntime().exec("notepad.exe"); // cause this process to stop until process p is terminated p.waitFor();//导致main主线程堵塞,直到Process p中断terminated,继续主线程继续运行下去 // when you manually close notepad.exe program will continue here (导致mian主线程堵塞,直到) System.out.println("Waiting over."); } catch (Exception ex) { ex.printStackTrace(); } } }
result:
Creating Process...
(堵塞中。。。直到关闭note 输出waiting over,此时如果调用Process.exitValue(),可以得到0(程序正常结束) 1(非正常中断)) Waiting over.
相关文章推荐
- 获取Java程序运行的路径
- Java 如何获取程序运行时的相对路径
- JavaSE__获取Java程序运行的路径 | 获取当前jar包的路径
- JAVA获取运行程序的src路径
- c# 设置winform程序为默认打开软件 在运行中获取参数
- 获取Java程序运行的路径
- Java获取程序运行时间
- java打开指定文件(运行指定程序exe)
- 获取java程序运行时内存信息
- Linux后台启动程序以及打开XShell连接运行java -jar然后关闭连接程序不再运行问题
- 获取Java程序运行路径
- 设置环境变量实现使用“运行“快速打开QQ等程序
- 打开程序开始→运行→命令集锦Strut2教程-java教程
- 获取Java程序运行的路径 | 获取当前jar包的路径
- java——获取一个应用程序运行的次数,如果超过5次,给出使用次数已到请注册的提示,并不要再运行程序
- Java桌面程序多次被打开时显示已运行的程序的窗口
- 获取Java程序运行的路径 | 获取当前jar包的路径
- JAVA程序获取Tomcat的运行状态
- Linux后台启动程序以及打开XShell连接运行java -jar然后关闭连接程序不再运行问题
- 获取Java程序运行的路径 | 获取当前jar包的路径