0

I need to launch the xterm from my java application and send command to it. This xterm needs to be opened for a while and run commands on it on-demand. Read the output from it, is another use-case.

When I check for the solution, everyone is suggesting this : [https://stackoverflow.com/questions/15356405/how-to-run-a-command-at-terminal-from-java-program?noredirect=1&lq=1] But my requirement is bit different.

String[] command= {"/bin/bash", "-c", "xterm"}; 
Runtime rt = Runtime.getRuntime();  
Process pr = rt.exec(command);
BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
ReadThread input = new ReadThread(in);
input.start();

PrintWriter printWriter = new PrintWriter(new BufferedWriter(new 
OutputStreamWriter(pr.getOutputStream())),true);
WriteThread writeThread= new WriteThread(printWriter);

// This 'writeThread' thread send commands based on some event like below
// printWriter.println("ls -a\n");

Read thread actually reading from the process that launched xterm, not directly from xterm. Read message is some thing like this: "Stdout: Warning: Tried to connect to session manager, Could not open network socket"

There is another issue too. How can I directly communicate with launched Xterm from java. I have also tried to use '-into' to add xterm to Java native window. This makes the xterm non-interactive.

6
  • 1
    Xterm has no interesting standard input or standard output, so communicating with it is a bit difficult. Can you explain why you need it? Commented Dec 12, 2024 at 15:45
  • Thanks! We have to interact with a model that tells the Java to issue multiple commands in several terminals to run some test-cases and other stuff. And also expects the result sometime. There is no way we can communicate with it? Are there any other helpful terminals? Commented Dec 12, 2024 at 15:53
  • 2
    I wonder where exactly the xterm or any terminal requirement is hidden in your statement. I don't see any. If you want to communicate with a program from your app, just have the app run it. Commented Dec 12, 2024 at 16:06
  • 1
    Reeks of X-Y problem ... what is the app you need to communicate with, what does it do? Why xterm? Commented Dec 12, 2024 at 16:50
  • What you appear to want to do is write to the sub-process of /bin/bash -c xterm - but that is inaccessible with getOutputStream(). If you ran bash as the sub-process you should be able to send it commands. Commented Dec 12, 2024 at 17:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.