Skip to content
Advertisement

Start an external process while stopping current process

Not sure what’s the right word for this or if this is possible.
I would like to start an external process and stop the current process in the same terminal window.
(I would like to avoid piping I/O streams for the child process.)

public static void main(String[] args) {
    String ip = chooseFromCommandLine();
    String cmdLine = "ping " + ip;

    // launch cmdLine in the same terminal and exit this process
}

Essentially to create a “launcher”-type application but for the terminal.

Advertisement

Answer

In UNIX / Linux / POSIX, the terminology for this is “execing” the application. The current executing process is replaced with a new application.

Unfortunately, you can’t do that in pure Java. You may be able to do it from native code that you call from Java.

Java’s Runtime.exec(...) etcetera methods do the equivalent of a POSIX fork followed by exec in the child process. In other words, the parent process (i.e. the JVM) keeps running.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement