0

I am testing an application. My test is complex, and I spawn 2 thread that start 2 process builders which spawn 2 java processes.

Is it possible to write a custom redirect that will be similar to inherit but prepend something to every out and err message, so that I would know its origin.

Example code below:

public class test {


    public static void main(String... args){


        Thread t = new Thread(new testHelper());
        t.start();

        t = new Thread(new testHelper());
        t.start();

    }

}

import java.io.IOException;


    public class testHelper implements Runnable {
        @Override
        public void run() {
            Class klass = testWorker.class;



            System.out.println(klass.getCanonicalName());
            String separator = System.getProperty("file.separator");
            String classpath = System.getProperty("java.class.path");
            String path = System.getProperty("java.home")
                    + separator + "bin" + separator + "java";
            ProcessBuilder processBuilder =
                    new ProcessBuilder(path, "-cp",
                            classpath,
                            klass.getCanonicalName());
            processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
            processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
            Process process = null;
            try {
                process = processBuilder.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                process.waitFor();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Child Process is done");


        }
    }

public class testWorker {

    public static void main(String ... args) throws InterruptedException {


        System.out.println("Doing some stuff");
        Thread.sleep(10000);
        System.out.println("Finished doing some stuff");



    }

}

1 Answers1

1

No, its not possible. In the source code for java.lang.ProcessBuilder.Redirect the constructor is private and has this to say

/**
 * No public constructors.  Clients must use predefined
 * static {@code Redirect} instances or factory methods.
 */
 private Redirect() {}
heldeen
  • 411
  • 4
  • 6