Hello All.

I have a program that automatically restarts itself atfer a certain amount of time.
I am doing this to test some things out. The program is wrapped in a jar.
I run the jar from terminal with
java -jar ProgramName.jar

It will run and will output all the "System.out.println" outputs in terminal

When the program restarts itself, It stops outputing on terminal. It runs fine and everything, I just want to see the output. How would I accomplish this.

this is my restarting code.

public void Restart() throws IOException {
        try {
            final String javaBin = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java";
            final File currentJar = new File(ProgramName.class.getProtectionDomain().getCodeSource().getLocation().toURI());

            /* is it a jar file? */
            if (!currentJar.getName().endsWith(".jar")) {
                return;
            }

            /* Build command: java -jar application.jar */
            final ArrayList<String> command = new ArrayList<>();
            command.add(javaBin);
            command.add("-jar");
            command.add(currentJar.getPath());

            final ProcessBuilder builder = new ProcessBuilder(command);
            builder.start();
            System.exit(0);
        } catch (URISyntaxException ex) {
            Logger.getLogger(ProgramName.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

Any help would be appreciated.

Have you tried redirecting the new process's output stream? There are various options for that (see the API doc). This one looks particularly interesting...

public ProcessBuilder inheritIO()
Sets the source and destination for subprocess standard I/O to be the same as those of the current Java process.

Perfect.

public ProcessBuilder inheritIO() was the perfect solution!!!!! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.