Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

process.getInputStream() and process.waitfor() block in web application

807603Apr 17 2007 — edited Feb 13 2008
Hi folks,

i am really stuck with a problem which drives me mad.....

What i want:

I want to call the microsoft tool "handle" (see http://www.microsoft.com/technet/sysinternals/ProcessesAndThreads/Handle.mspx) from within my web-application.
Handle is used to assure that no other process accesses a file i want to read in.

A simple test-main does the job perfectly:
public class TestIt {

   public static void main(String[] args){

      String pathToFileHandleTool = "C:\\tmp\\Handle\\handle.exe";
      String pathToFile = "C:\\tmp\\foo.txt";
      String expectedFileHandleSuccessOutput = "(.*)No matching handles found(.*)";
      System.out.println("pathToFileHandleTool:" + pathToFileHandleTool);
      System.out.println("pathToFile: " + pathToFile);
      System.out.println("expectedFileHandleSuccessOutput: " + expectedFileHandleSuccessOutput);
      
      ProcessBuilder builder = null;
      // check for os
      if(System.getProperty("os.name").matches("(.*)Windows(.*)")) {
         System.out.println("we are on windows..");
      } else {
         System.out.println("we are on linux..");
      }
      builder = new ProcessBuilder( pathToFileHandleTool, pathToFile);
      Process process = null;
      String commandOutput = "";
      String line = null;
      BufferedReader bufferedReader = null;
      try {
         process = builder.start();
         // read command output
         bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

          while((line = bufferedReader.readLine()) != null) {
             commandOutput += line;
          }
          System.out.println("commandoutput: " + commandOutput);
         // wait till process has finished
         process.waitFor();
      } catch (IOException e) {
         System.out.println(e.getMessage());
         e.printStackTrace();
      }  catch (InterruptedException e) {
         System.out.println(e.getMessage());
         e.printStackTrace();      }
      // check output to assure that no process uses file
      if(commandOutput.matches(expectedFileHandleSuccessOutput))
         System.out.println("no other processes accesses file!");
      else
         System.out.println("one or more other processes access file!");
   }
} 
So, as you see, a simple handle call looks like
handle foo.txt
and the output - if no other process accesses the file - is:
Handle v3.2Copyright (C) 1997-2006 Mark RussinovichSysinternals - www.sysinternals.com
No matching handles found.
no other processes accesses file!
(Wether the file exists or not doesnt matter to the program)

If some processes access the file the output looks like this:
 commandoutput: Handle v3.2Copyright (C) 1997-2006 Mark RussinovichSysinternals - www.sysinternals.com
WinSCP3.exe        pid: 1108    1AC: C:\tmp\openSUSE-10.2-GM-i386-CD3.iso.filepart
one or more other processes access file!
So far, so good.........but now ->

The problem:

If i know use the __exact__ same code (even the paths etc. hardcoded for debugging purposes) within my Servlet-Webapplication, it hangs here:
 while((line = bufferedReader.readLine()) != null) {
if i comment that part out the application hangs at:
process.waitFor();
I am absolutely clueless what to do about this....

Has anybody an idea what causes this behaviour and how i can circumvent it?
Is this a windows problem?

Any help will be greatly appreciated.....

System information:

- OS: Windows 2000 Server
- Java 1.5
- Tomcat 5.5


More information / What i tried:

- No exception / error is thrown, the application simply hangs. Adding
builder.redirectErrorStream(true);
had no effect on my logs.

- Tried other readers as well, no effect.

- replaced
while((line = bufferedReader.readLine()) != null)
with
int iChar = 0;
	    	while((iChar = bufferedReader.read()) != -1) {
No difference, now the application hangs at read() instead of readline()

- tried to call handle via
runtime = Runtime.getRuntime();			
Process p = runtime.exec("C:\\tmp\\Handle\\handle C:\\tmp\\foo.txt");
and
Process process = runtime.exec( "cmd", "/c","C:\\tmp\\Handle\\handle.exe C:\\tmp\\foo.txt");
No difference.

- i thought that maybe for security reasons tomcat wont execute external programs, but a "nslookup www.google.de" within the application is executed

- The file permissions on handle.exe seem to be correct. The user under which tomcat runs is NT-AUTORIT-T/SYSTEM. If i take a look at handle.exe permission i notice that user "SYSTEM" has full access to the file

- I dont start tomcat with the "-security" option

- Confusingly enough, the same code works under linux with "lsof", so this does not seem to be a tomcat problem at all

Thx for any help!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 12 2008
Added on Apr 17 2007
8 comments
1,055 views