我有一个JAVA代码片断,需要调用一个外部文件,如果这个文件已经运行,就显示有关信息,如果没有运行,则运行它!
if(?)
System.out.println("read.exe is running!!!");
else
Process p= Runtime.getRuntime.exec("cmd read.exe");
if(?)
System.out.println("read.exe is running!!!");
else
Process p= Runtime.getRuntime.exec("cmd read.exe");
http://www.microsoft.com/downloads/details.aspx?familyid=49AE8576-9BB9-4126-9761-BA8011FABF38&displaylang=zh-cn然后用下面程序进行判断
import java.io.BufferedReader;
import java.io.InputStreamReader;public class findProcess {
public static void main(String args[]) {
try {
// pstat.exe is tool from microsoft, download the free "support
// tools"
Process pr = Runtime.getRuntime().exec("pstat");
BufferedReader br = new BufferedReader(new InputStreamReader(pr
.getInputStream()));
String temp = br.readLine();
while (temp != null) {
System.out.println(temp);
if (temp.indexOf("process name") > 0) {
System.out.println(temp + " found");
break;
}
temp = br.readLine();
}
} catch (Exception e) {
}
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;public class findProcess {
public static void main(String args[]) {
try {
// pstat.exe is tool from microsoft, download the free "support
// tools"
Process pr = Runtime.getRuntime().exec("C:\\Program Files\\Support Tools\\pstat.exe");
BufferedReader br = new BufferedReader(new InputStreamReader(pr
.getInputStream()));
String temp = br.readLine();
while (temp != null) {
System.out.println(temp);
if (temp.indexOf("javaw") > 0) {
System.out.println(temp + " found");
break;
}
temp = br.readLine();
}
} catch (Exception e) {
}
}
}