0

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
user
  • 6,567
  • 18
  • 58
  • 85
  • Simple Library to make the process of executing system commands through java a simple task. The library is thread-safe, can be used to execute multiple commands asynchronously.` https://github.com/mhashim6/System-Command-Executor ` – HariKishore Jan 20 '21 at 10:04
  • 1
    @HariKishore - That actually does something a bit different. The current process is not **replaced**. – Stephen C Jan 20 '21 at 10:09
  • Please don't re-tag the question to change the topic matter. If you want to ask about how to do this in C or shell or something else, **ask a new question**. (And search for existing Q&A's first.) – Stephen C Jan 20 '21 at 10:22
  • it was always about execing, java is just a tool (the wrong tool in this instance) – user Jan 20 '21 at 10:28
  • I wanted to know how to do it easily, I don't want to fight with the language, the JVM. – user Jan 20 '21 at 10:37

1 Answers1

1

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 1
    I just noticed I answered a similar question 6 years ago: https://stackoverflow.com/questions/22658894. – Stephen C Jan 20 '21 at 10:12
  • java is not a strong requirement for me, is it possible to exec in a "portable" way? (so it works on at least linux and windows.) is it just an exec call in C? – user Jan 20 '21 at 10:15
  • Ask a new question. This question was asked and answered about Java – Stephen C Jan 20 '21 at 10:20