how to get Process object from a thread (Runnable)

M

Marteno Rodia

Hello,
this question follows a similar question I've already asked before,
but now it will bo more specyfic. Let's imagine I have some code (a
part of a bigger program) for which I want to redirect output and
error streams into a log file. I can do this using
Process.getErrorStream() and Process.getInputStream() functions. I can
also put the appropriate part of my code (which in fact has not been
written by me) into the run() method of an anonymous class
implementing the Runnable interface, but... there is one problem:

How to get a Process object out of the Runnable object? Is it possible
by any means?

MR
 
N

Nigel Wade

Marteno said:
Hello,
this question follows a similar question I've already asked before,
but now it will bo more specyfic. Let's imagine I have some code (a
part of a bigger program) for which I want to redirect output and
error streams into a log file. I can do this using
Process.getErrorStream() and Process.getInputStream() functions.

A Process object refers to an external executing process with its own
input/output streams. Process's getErrorStream/getInputStream allows the JVM to
read the external Process's output and error streams. It doesn't allow you to
redirect the JVM's output and error streams.

I believe you may be able to do this by opening a file and then using
System.setErr/setOut, but you'd need to check that. However, I'm sure you'd be
better off investigating the use of a Logger.
I can
also put the appropriate part of my code (which in fact has not been
written by me) into the run() method of an anonymous class
implementing the Runnable interface, but... there is one problem:

How to get a Process object out of the Runnable object? Is it possible
by any means?

A Runnable is code to be executed on a thread within the JVM process. If you
could get a Process associated with a Runnable it would be the JVM process. If
you redirect the output for your Runnable code you also redirect it for all
other threads in the JVM because all threads are executed by the JVM and share
the same input/output/error streams.
 
J

John B. Matthews

"Matt Humphrey said:
Wow--that's a typo. Runnables AND Process are not related. Sheesh!

The typo was easy to figure out, but I was less sanguine about the
notion of "fragment of code". If I may amplify: A class that implements
the Runnable interface "must define a method of no arguments called
run." [1] If a Thread is constructed with an instance of a Runnable
class, the run() method is invoked when the Thread is started. [2, 3]

Of course, as you helpfully observed, the OP can execute arbitray Java
code in a separate virtual machine, if desired:

Process p = Runtime.getRuntime().exec("java MyRunnable"); [4]

[1]<http://java.sun.com/javase/6/docs/api/java/lang/Runnable.html>
[2]<http://java.sun.com/javase/6/docs/api/java/lang/Thread.html>
[3]<http://java.sun.com/docs/books/tutorial/essential/concurrency/runthread.html>
[4]<http://groups.google.com/group/comp.lang.java.programmer/msg/3a52ecfb9ab8fc1f>
 
L

Lew

Daniel said:
do you have your own logging infrastructure?
...
Of course, the access to the logging object should be somehow synched

An advantage of both log4j and the inbuilt java.util.logging framework is that
they do the synching for us.
 
M

Marteno Rodia

Please clarify:
- you got some functionality

yes, definately
- you want it multithreaded or to run in a separate thread

To be precise, I have a multi-threaded application, and I want to
supress messages just for a part of one thread. The code which is
executed here hasn't been written by me.
- you want your threads to share an object of class Process that will
serve the purpose you mentioned.

I want to get a Process object just for this part of one of my
threads. I could launch another Thread for it, but the problem is
still I cannot get the Process object.

***
The trick with loggers is also not a good idea, because I don't want
to/can't modify not my own code.

MR
 
N

Nigel Wade

Marteno said:
yes, definately


To be precise, I have a multi-threaded application, and I want to
supress messages just for a part of one thread. The code which is
executed here hasn't been written by me.


I want to get a Process object just for this part of one of my
threads. I could launch another Thread for it, but the problem is
still I cannot get the Process object.

As has already been pointed out to you the process associated with each thread
within the JVM is the JVM itself. Since you already have direct access to the
input/output streams of the JVM via System.out/System.in/System.err you don't
need a Process to get these Streams. However, those System streams are shared
between all threads, so you can't redirect them just for some code executed by
a thread, they will be redirected for all threads for the duration of the
redirect.
***
The trick with loggers is also not a good idea, because I don't want
to/can't modify not my own code.

Then I'm pretty sure you can't do what you are attempting to do by any simple
means.

You may be able to implement your own class to handle the output by extending
PrintStream so it could replace System.out/err. You would need to devise your
own mechanism within that class for redirecting only the output generated by
the code you haven't written. But don't ask me how...
 
M

Marteno Rodia

Then I'm pretty sure you can't do what you are attempting to do by any simple
means.

The only way to do that I can think about has been already mentioned
in the thread. It involves making from "the part of the code" an
external application and invoking it as a Process object. It is
possible, however, I don't like it, because I must export the project
into JAR file, and then invoke the JVM to execute this JAR, and what
if a user of the application moves or deletes the external JAR? How
can I ensure in another way MyClass to be visible in the working
directory?

Anyway, thanks for all your responses up tu now and later :))

MR
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,264
Latest member
FletcherDa

Latest Threads

Top