13

Say I have a current running process known, how can I turn this into a Process object in Java? The process is already running, so I don't want to spawn off another one, I just want to encapsulate it into a Process object that I can use within the java code. Something along the lines of:

int pid = getPid();
Process proc = magicGetProcess(pid);

thanks

4
  • I don't think Java has sufficient process management capabilities built-in. Hopefully someone will prove me wrong. Commented Oct 21, 2010 at 21:03
  • Which of the functionalities of Process do you most want? Are you trying to do a waitFor(), or are you looking to be able to redirect the input/outputstreams to your app, or what? I'm pretty sure what you're asking for in itself is impossible but we might be able to help with your actual requirements. Commented Oct 21, 2010 at 21:03
  • Well I need to pass in this process into a method that pretty much checks if it unexpectedly terminated and grabs the exit code if it did. Commented Oct 21, 2010 at 21:07
  • See related: stackoverflow.com/q/8257742/435605 Commented Mar 23, 2013 at 17:53

4 Answers 4

4

I don't think this is possible using only the builtin library. AFAIK, it is already non-trivial to get the running process' own PID (see the feature request and alternate mechanisms).

A quick look at the java.lang.Process class shows that you could go about writing your custom implementation of java.lang.Process using JNI and native code. Your custom class could then implement extra methods, such as the one in your question.

Sign up to request clarification or add additional context in comments.

2 Comments

Hmm ya that is what it looks like. Oh well.
FYI: broken url for java.lang.Process
0

In *nix world grabbing exit code of a non-child process is not easy, because the exit code simply disappears together with the process as soon as the parent process has picked up the exit code of the child. You can attach to the running process using some tracing tool and pick up its exit code when the process dies. Most *nix OSes have command line tools which will let you do it (such as strace on Linux, truss on SunOS) in a non-intrusive way. However, you can only use them against your own processes or if you run as root. Another alternative is to configure audit subsystem of your OS to record exit codes of all processes.

1 Comment

On Unix you can use the waitpid() function to grab the exit code of a non-child process. The problem is calling it from Java.
0

You can't. Every operation in Process requires that the process is a child process. Not an arbitrary process.

Comments

0

what about using RMI? It it possible to pass a Process object to the Process which it is? Probably not, because Process is not Serializable

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.