6

I am trying to open a shell (xterm) and interact with it (write commands and read the shell's output)

Here is a sample of code which won't work:

public static void main(String[] args) throws IOException {
 Process pr = new ProcessBuilder("xterm").start();
 PrintWriter pw = new PrintWriter(pr.getOutputStream());
 pw.println("ls");
 pw.flush();
 InputStreamReader in = new InputStreamReader(pr.getInputStream());
 System.out.println(in.read());
}

When I execute this program an "xterm" window opens and the "ls" command is not entered. Only when I close the window I get a "-1" printed and nothing is read from the shell

IMPORTANT-

I know I can just use:
Process pr = new ProcessBuilder("ls").start();

To get the output, but I need the "xterm" opened for other uses

Thanks a lot

asked Nov 17, 2012 at 14:26
1
  • The title is misleading. An xterm is a terminal application that runs a shell. It isn't a shell itself. Commented Nov 17, 2012 at 15:17

2 Answers 2

5

Your problem is that the standard input and output of the xterm process don't correspond to the actual shell that is visible in the terminal window. Rather than an xterm you may have more success running a shell process directly:

Process pr = new ProcessBuilder("sh").start();
answered Nov 17, 2012 at 14:36
Sign up to request clarification or add additional context in comments.

1 Comment

I know this is years old but other people will see this. This solution does not work by itself. For what you want to work you need to add -i to make it interactive. I have been pounding my head against for an hour before I realized I should just check the docs for /bin/sh pubs.opengroup.org/onlinepubs/009695399/utilities/sh.html
2

Here a full java main example of how to interact with shell on java 8 (is really simple do that on java 4,5,6 whatever)

Example of output

$ javac Main.java
$ java Main
echo "hi"
hi

The code

import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Main {
 public static void main(String[] args) throws IOException, InterruptedException {
 final List<String> commands = Arrays.asList("/bin/sh");
 final Process p = new ProcessBuilder(commands).start();
 // imprime erros
 new Thread(() -> {
 BufferedReader ir = new BufferedReader(new InputStreamReader(p.getErrorStream()));
 String line = null;
 try {
 while((line = ir.readLine()) != null){
 System.out.printf(line);
 }
 } catch(IOException e) {}
 }).start();
 // imprime saida
 new Thread(() -> {
 BufferedReader ir = new BufferedReader(new InputStreamReader(p.getInputStream()));
 String line = null;
 try {
 while((line = ir.readLine()) != null){
 System.out.printf("%s\n", line);
 }
 } catch(IOException e) {}
 }).start();
 // imprime saida
 new Thread(() -> {
 int exitCode = 0;
 try {
 exitCode = p.waitFor();
 } catch(InterruptedException e) {
 e.printStackTrace();
 }
 System.out.printf("Exited with code %d\n", exitCode);
 }).start();
 final Scanner sc = new Scanner(System.in);
 final BufferedWriter bf = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
 final String newLine = System.getProperty("line.separator");
 while(true){
 String c = sc.nextLine();
 bf.write(c);
 bf.newLine();
 bf.flush();
 }
 }
}
answered Feb 8, 2016 at 2:07

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.