2

Regarding the topic, the code below


    Process proc = null;
    try {
        String[] cmdss= {"gnome-terminal"};


        proc = Runtime.getRuntime().exec(cmdss, null, wd);
    } catch (IOException e) {
        e.printStackTrace();
    }

Runs the terminal form Ubuntu.

How do I issue commands into the terminal after running the termnal?

eg: running the terminal and run command such as "ls" etc.

  • You should do whatever you're trying to do in Java instead of using shell commands. – SLaks Sep 28 '11 at 17:04
  • hi, i would love to do that but i do not think that's an option for me. This is because apart from running the scripts, user may need to input commands such as y/n into the terminal after running certain scripts. I am not sure how to do that from java interface. – Melvin Chan Kok Leng Sep 28 '11 at 17:11
  • 1
    What are you _actually_ trying to do? You should ask a separate question? – SLaks Sep 28 '11 at 17:14
  • an example: running a fortran program from java. If i would run the fortran program entirely from java exec, i have no problem with running it. However, the fortran program may prompt user for extra input. In this case, i do not think java is able to capture termninal "waiting for input" state and prompt the use to enter data from java interface. – Melvin Chan Kok Leng Sep 28 '11 at 17:22
  • maybe duplicate: http://stackoverflow.com/questions/525212/how-to-run-unix-shell-script-from-java-code – thomas Sep 28 '11 at 17:42

1 Answers1

2

You can give gnome-terminal some options on the command line what it shall execute.

gnome-terminal -e /my/fortran/program

The -x option gives you roughly the same benefit but you can split the commandline into separate words.

Both -e and -x run the program with optional arguments while connecting the program`s standard input and output to the terminal. So the user can interact with the terminal properly.

Example:

gnome-terminal -x bash -c "ls; echo '<enter>'; read"

This will open the terminal and run the "program" bash. bash will get two arguments: -c and ls; echo ....; read. The -c option makes bash parsing and executing the next argument. This will call ls, then echo ... then read which waits for the return key.

In Java you must split the arguments appropriately into an array like this:

String cmd[] = {"gnome-terminal", "-x", "bash", "-c", "ls; echo '<enter>'; read" };
A.H.
  • 63,967
  • 15
  • 92
  • 126
  • You also might consider using a program like expect so you can script the responses for interactive prompts; which seems like it's really what you're after. – Kevin Sep 28 '11 at 17:58
  • Kevin, that was my real intention. However, i have no clue how to do that since i am a .Net/Windows based programmer but not a java/Unix programmer. So i am completely clueless on "expect" thing. – Melvin Chan Kok Leng Sep 28 '11 at 18:02
  • I understand the description at least partially. "[I]t works exactly like i wanted" surprises me a bit, because I don't recognize in what's described much of a model for the user-interaction-with-Fortran that I thought Melvin specified. In any case, it's possible to manage interaction with a Fortran-coded process either through a terminal, or as a direct subprocess of Java; I *strongly* favor the latter. There's abundant Expect help around for you, Melvin. http://expectj.sourceforge.net/, http://stackoverflow.com/questions/tagged/expect, and http://wiki.tcl.tk/expect might interest you. – Cameron Laird Sep 28 '11 at 19:11