2

I had previously asked the same question but with no answer, and have found some other questions similar to this problem here and here but again with no appropriate answers. Can anyone please help me out with this. I am trying to open a shell from Java and interact with it (write commands and read the shell's output). The commands will be given by the user like changing directory, compiling a C program etc. The command list is not fixed.

I have also tried and use the /bin/bash -c method and the following as well.

Process p = new ProcessBuilder("xterm").start();

Process p = new ProcessBuilder("/bin/bash").start();

Thanks and I hope that the problem is clear.

5
  • Should your Java program be a "director" for the shell being executed, or it's intended to run a shell and provide a user ability to interact with it? Commented Feb 17, 2014 at 7:11
  • I want to start the shell with some directory redirected (Java will change the directory of the shell using cd command). Once started, I want that the user can interact with it. Commented Feb 17, 2014 at 7:19
  • If you want to start an interractive shell, you really need to start a virtual terminal, just the shell is not enough. The shell only processes commands, starts processes and ties them together, but it isn't what displays the output in a box Commented Feb 17, 2014 at 7:58
  • Can you explain what you need this for? Maybe there is another way to do it. There are several java implementations of interactive shells, such as java shell java-shell.sourceforge.net rhino developer.mozilla.org/en/docs/Rhino/Shell and beanshell.org Commented Feb 17, 2014 at 8:07
  • I wish to develop a C program profiler in Java. For proper module development it will be useful if the user can compile and execute the C code via the GUI provided itself without separately opening the bash terminal. Commented Feb 18, 2014 at 5:49

1 Answer 1

1

Example of workable solution will be almost like below

   Process process;

   rt = Runtime.getRuntime();

try 
{

    process = rt.exec(new String[]{"bash","-c","ls -al /home/"});

    log.warn("message to display");

    BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));

    String line=null;

    while((line=input.readLine()) != null) {

        log.warn(line);
    }

    int exitVal = process.waitFor();

    log.warn("Exited with error code : "+ exitVal);

}
catch (IOException e) 
{
    log.warn("IO Execption 1 Happen : " + e.getMessage());
} 
catch (Exception e) 
{
    log.warn("Execption Happen : " + e.getMessage());
}
Sign up to request clarification or add additional context in comments.

2 Comments

This doesn't do exactly what the poster asked for, but I still think it's cool that it worked.
I had already tried this method, but still thanks for trying.

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.