Вопрос

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