1

first of all, I found a lot of questions about xterm and java, but no questions handles my problem directly.

What is my problem? I want to start a xterm terminal from java and I want to send commands to this terminal. First I just want to change the directory, but it does not work. But it is important, that I don't know all commands at the beginning of the program, so it is recommended, that I can send commands to the terminal at run-time.

Here is my code:

String[] command= {"xterm"}; Runtime rt = Runtime.getRuntime(); Process pr = rt.exec(command); Thread.sleep(2000); BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream())); ReadThread input = new ReadThread(in); input.start(); BufferedReader error = new BufferedReader(new InputStreamReader(pr.getErrorStream())); ReadThread inputError = new ReadThread(error); inputError.start(); PrintWriter printWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(pr.getOutputStream())),true); printWriter.println("cd /home/***/sipp/sipp-3.3\n"); Thread.sleep(2000); input.die(); inputError.die(); printWriter.close(); error.close(); in.close(); pr.destroy(); 

I thought that the terminal will open (it does) and change the directory to sipp-3.3 after 2 seconds. Another 2 seconds later the xterm should close (it does). But what is the problem, that my command does not work? And please I don't want to find a solution like

String [] gggg = {"xterm", "-c", "multiple commands, with |, &&, ; etc"}; rt.exec(gggg); 

Because with a solution like this, I am not able to send further commands to the terminal. Many thanks in advance!

4
  • Why don't you directly launch the commands without using xterm? Commented Nov 25, 2013 at 9:03
  • And what does "my command does not work" mean? What would you expect to happen? Commented Nov 25, 2013 at 9:28
  • The purpose of xterm is, that the user can see within the terminal what happens and that he can interact with the terminal manually. Furthermore I want to start a program (sipp) which needs a terminal as background. I expect that I can see the changed directory in the xterm. So that I can go on and start the program Commented Nov 25, 2013 at 9:33
  • This is long back. Still I have requirement something like this. Anyone has the solution now? I found lot of these solutions. stackoverflow.com/questions/15356405/… I need to open xterm on demand and based on other request, run command on the already opened xterm. Not sure how can this be done. Commented Dec 5, 2024 at 15:29

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.