我有一个JAVA代码片断,需要调用一个外部文件,如果这个文件已经运行,就显示有关信息,如果没有运行,则运行它!
if(?)
   System.out.println("read.exe is running!!!");
else
  Process p= Runtime.getRuntime.exec("cmd  read.exe");

解决方案 »

  1.   

    设置一个全局变量,调用这个文件的时候将这个变量设为true,操作结束后再设为false;
      

  2.   

    如果你的系统是xp,下载Windows XP Service Pack 2 支援工具 
    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) {
    }
    }
    }
      

  3.   

    安装sp2支援工具,主要使用pstat.exe程序
      

  4.   

    刚才试了一下,用这个是可以的
    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) {
    }
    }
    }
      

  5.   

    在SERVER 2003或 WIN2K SERVER 下能行么?