2 Replies Latest reply: Mar 24, 2009 2:44 AM by 807588 RSS

    How to get the process blocking a file

    807588
      Hi all,

      How can we list all processes blocking/using a file ?
      Is it possible ?

      Any type of input welcome.

      Kaushalya
        • 1. Re: How to get the process blocking a file
          577611
          I've personally never done this in Java, but I have used an application called Process Explorer. Google it. It can search for processes that have locked a particular file.
          • 2. Re: How to get the process blocking a file
            807588
            I think you can not find out the process using a file. but i can give you an idea by which you can explore all process on your system By which youcan sort out which process is using file by looking at the process.
            1. Create a RunTime <Runtime run = Runtime.getRuntime();>
            2. create a process for command <TaskList>
            3. retrive it in bufferedReader
            4. Now you have all process running on you system in your hand.
            5. find out which is using file by the name of the process.


            try {   
            Runtime run = Runtime.getRuntime();
            String locsasexe = "TASKLIST" ;
            Process pp = run.exec(locsasexe);
            BufferedReader in = new BufferedReader(new InputStreamReader(pp.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
            System.out.println(line);
            }
            int exitVal = pp.waitFor();
            System.out.println(exitVal);
            } catch (Exception ex) {
            ex.printStackTrace();
            }