简体   繁体   中英

Alternative to Runtime.exec() for a full Console java application?

I've been trying to code an app that allows me to open multiple Console sessions (in windows that would be cmd.exe) in a Tabbed fashion way.

One of the most important references I've found is in the following URL where I was able to get a TextAreaOutputStream code to be able to read the STDOUT fron the Process, but this and most of the references around the internet does not solve/fix the issue I'm dealing with.

Reference: Create Java console inside a GUI panel

Even when redirecting the STDOUT, STDERR and STDIN caused me some troubles, the worst part comes when the command you type within the Java Console in one of the tabs executes a command that creates a child process, it's here when my Java applications loses control because a new process was created in the background. Basically, the new child process is out of bound and I'm not able to receive or send any bytes to it and my app hangs.

  • JVM
    • MyTabbedConsoleApp
      • cmd.exe /k (I can still communicate with this one, no problems until here)
        • apptrack.exe (a child process out of bound from my app, no communication with this, the app hangs)

I'm looking for a better way to create an app that pretty much mimics the Console environment.

You should use ProcessBuilder instead of Runtime as it is the recommended API to deal with operating systems processes.

Check the Changes to Runtime.exec topic for the Java 6u45 Release Notes.

The preferred way to create operating systems processes since JDK 5.0 is using java.lang.ProcessBuilder. The ProcessBuilder class has a much more complete API for setting the environment, working directory and redirecting streams for the process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM