I have a Java program that needs to launch an interactive command line interface C program in the foreground. I need this to work in both Windows and Linux.

In Windows, everything works fine. I am able to launch my program in the foreground with cmd as follows:

command = "cmd /c start \"My_C_Program\" /MAX /WAIT My_C_Program ";
theProcess = Runtime.getRuntime().exec(command);

In Linux, I am unable to do the same with /bin/bash. No matter what I try, the process always runs in the background:

String[] commands = { "/bin/bash", "-c", "./My_C_Program" };
theProcess = Runtime.getRuntime().exec(commands);

I know that I can retrieve the output from the background process but this is useless since the program is interactive.

Any help would be greatly appreciated. Even elaborate workarounds :)

有帮助吗?

解决方案

You can try this instead:

String[] commands = { "gnome-terminal", "-x", "-c", "./My_C_Program" };
theProcess = Runtime.getRuntime().exec(commands);

Not sure will it work. Didn't try it.

Maybe you don't have gnome-terminal but something else like x-term. It should be almost the same.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top