How can I launch VI from within Java under commons-exec?

后端 未结 3 863
孤街浪徒
孤街浪徒 2020-12-20 23:50

I would like to be able to launch VI from within my Java program and wait for the user to quit VI before proceeding. Here\'s the code snippet that I have currently:

3条回答
  •  醉酒成梦
    2020-12-21 00:50

    When Java runs a program via Runtime.exec() (and this is what commons-exec does in the end), it connects the program's input, output and error streams to your Java app as input/output streams. Such a stream is certainly not a terminal, you can't for example move the text cursor in it (since it doesn't have any), change text colors, or detect if Shift key is pressed (since it's just a stream of bytes and not a physical keyborad). So, an interactive app like vi can't really function under such conditions like in a terminal.

    By the way, I'm not sure if the command line args you supply are parsed by the shell or passed directly to the program. In the latter case your redirection to /dev/tty couldn't possibly work even if there was a way for Java to somehow allow the program to replace Java's connected streams with something else.

    As an aside, it seems a bit strange why you would like to run vi from inside a Java program.

    So I guess the best solution is to execute a terminal emulator like konsole or gnome-terminal or xterm and let it run vi by passing corresponding argument on its command line (e.g. konsole -e vi). In this case the terminal's window should pop up and vi could function inside it. Of course, it won't work if you're on a headless server, but then running vi can't be useful anyway.

提交回复
热议问题