2
import java.io.*;

public class GnomeCommand {

    public static void main(String args[]) throws IOException {

        try {
            Runtime r = Runtime.getRuntime();
            String[] cmdArray = {"/usr/bin/gnome-terminal", "-e", "ps ax | grep gnome", " ; exec $SHELL"};
            r.exec(cmdArray).waitFor();
        } catch (InterruptedException ex){
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

}

The problem is I cannot keep the command prompt open. It closes immediately. I also used "exec $SHELL" command but the result is same. I want to keep the terminal open after executing the command.

ahadcse
  • 499
  • 3
  • 7
  • 16

1 Answers1

0

Your sub process ends when the command execution ends. In your case after ps and grep have finished executing your script will end.

Try with xterm with -hold option

xterm -e "ps ax | grep gnome" -hold

stivlo
  • 83,644
  • 31
  • 142
  • 199
  • I am sorry I am not getting any of your answer. My OS is Fedora. I do not know about xterm terminal. It would be helpful for me if you please just mention what will be inside my {} of cmdArray instead of my current one? – ahadcse Jun 13 '13 at 09:18
  • Experiment on the command line, not in Java, if you try your command on the command line, from another shell, it behaves exactly as you described. After you make it work on the command line, split it up in your Java exec call. – stivlo Jun 13 '13 at 10:22