0

I am trying to open a vi editor using Java code in an linux env (the java code is executed via shell script). The editor should open in foreground & become active terminal, while the java is should be running at Background.

I tried using both commands using :-

  1. String []command = {"xterm", "vi", "/home/user/test.txt"}; Process pr = Runtime.getRuntime().exec(command);

  2. Process p = new ProcessBuilder("vi", "/home/user/test35.txt").start();

In one of the above code, if check the ps -ef | grep vi, I am able to the process, but its running at background. I want to run it in foreground as an active terminal as user for his type the text into the editor. While the java will be running at the background. Any suggestion or snippet's.

I have referred this Open VIM with Java application , but still in vain.

Kiran
  • 183
  • 5
  • 19

2 Answers2

0

If you want to create a new xterm and execute a command in that terminal, you need to pass the command with -e. Try this:

ProcessBuilder pb = new ProcessBuilder("xterm", "-e", "vi", "/home/user/test.txt");
xiaofeng.li
  • 8,237
  • 2
  • 23
  • 30
  • Thank you for suggesting, tried changing both the 1) String []command = {"xterm","-e", "vi", "/home/hscpe/test.txt"}; Process pr = Runtime.getRuntime().exec(command); 2) Process p = new ProcessBuilder("xterm","-e", "vi", "/home/hscpe/test35.txt").start(); , but in vain , still no success. Also there is no process been shown – Kiran Jun 02 '17 at 06:02
0

The debug steps I did was I tried opening a terminal via command & use the same command in the Java code. Issue observed that I need to set DISPLAY=:0. if I am running via root user , for other user export DISPLAY was not needed

String []command = {"/usr/bin/xterm","-e", "vi", "/home/hscpe/test.txt"};
 Process pr = Runtime.getRuntime().exec(command);

Since I am running the java code via shell script I will be adding export in the shell script i.e export DISPLAY=:0. Now will try to make the editor as editable (Will stimulate by pressing I , i.e Insert by java robot).

I referred here & here

Kiran
  • 183
  • 5
  • 19