1

I have used a method waitFor of Process class, to execute some UNIX command. The command is executing successfully but it is returning 2, that according to convention means abnormal termination.

I googled and found that it depends on "source code of the program that is outputting this exit code". So I tried to find the source code for waitFor method, I came to know that it is a native method and defined in ProcessImpl class.

public native int waitFor();

So I searched for native implementation of this method from here. But I did not found the file that provide me the native implementation of waitFor method.

My question is how to find the file in which native implementation of this method is written?

Community
  • 1
  • 1
Vishrant
  • 15,456
  • 11
  • 71
  • 120
  • 1
    I'm not sure, but doesn't `waitFor` just return the exit code of the process? In JDK7, `waitFor()` is written in Java (using `waitForInterruptibly` which is native), and `waitFor()` returns `exitValue()` which calls `getExitCodeProcess()` which really looks like it's just the exit code of the process. So the process you're running did an `exit(2);` [if it's written in C] or something equivalent. I don't think the native `waitFor` source in JDK6 is going to help you. – ajb Jul 23 '14 at 16:20
  • The source code of Process,waitFor() doesn't have anything to do with why the execute program exited with code 2. You're barking up the wrong tee. – user207421 Jul 23 '14 at 17:47
  • thanks @ajb and EJP for replying, even though looking after the native implementation of `waitFor` method will not help me, still I wanted to know how to find in which **file** the native implementation of `waitFor` method is written, for time being I am least concerned about what waitFor returns, but yes I wanted to know the flow how that value is returned and from where it is returned. – Vishrant Jul 24 '14 at 09:46

1 Answers1

4

The source code mentioned refers to the source of the program you are executing with your Process object.

In your case, it is the UNIX command you are executing using the Process that is terminating with exit code 2, not the waitFor() method itself.

So to answer your actual question, the answer is: Within the source code of the JDK you are using, for example here is the source for OpenJDK6.

However, if your actual question is "Why is waitFor() returning a non zero exit code", then the answer is: The Process you are executing with Java has returned a non zero exit code, and the Process object is informing you via the return of the waitFor() method

Andrew Stubbs
  • 4,322
  • 3
  • 29
  • 48
  • 1
    @Andreq Stubbs thanks for replying, my question was **how to find the file** in which native implementation of this method is written? I searched in `openjdk-6-src-b28-04_oct_2013.tar\jdk\src\share\native\java` directory but I did not found the `C` or `CPP` file that implements waitFor method, which is declared native in `ProcessImpl` class – Vishrant Jul 24 '14 at 09:38