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.

5
  • You should do whatever you're trying to do in Java instead of using shell commands. Commented Sep 28, 2011 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. Commented Sep 28, 2011 at 17:11
  • 1
    What are you actually trying to do? You should ask a separate question? Commented Sep 28, 2011 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. Commented Sep 28, 2011 at 17:22
  • maybe duplicate: stackoverflow.com/questions/525212/… Commented Sep 28, 2011 at 17:42

1 Answer 1

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" };
Sign up to request clarification or add additional context in comments.

3 Comments

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, 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.
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. expectj.sourceforge.net, stackoverflow.com/questions/tagged/expect, and wiki.tcl.tk/expect might interest you.

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.