0

I want to open a new terminal window and execute my program in java code. So I use:

Runtime.getRuntime().exec("gnome-terminal -x /path/to/my/program arg1 arg2"); 

This is work fine in Ubuntu but unfortunately it is not work in CentOS, the problem is that the terminal window closed immediately.

I have search many time in stackoverflow but could not find out a solution. Thank you very much!!!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Kery
  • 513
  • 8
  • 22
  • What terminal emulator/desktop environment are you trying to do this for/under? – Tawnos Jan 06 '13 at 05:54
  • 3
    The newer alternative ProcessBuilder covers some of the brittleness of exec. – Joop Eggen Jan 06 '13 at 06:31
  • 3
    1) There are common mistakes made when creating new processes - as detailed in [When Runtime.exec() won't](http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html). The article is the *first* thing to check if a process fails. Implement *all* the tips, and even if doing so does not make the process work, it will provide much more detailed information on why it failed. 2) Use a `ProcessBuilder` to construct the process. 3) For more reliable results, make the `String` based arguments into a `String[]`. – Andrew Thompson Jan 06 '13 at 07:24
  • does anything unusual happen if you enter it on the command line directly yourself? – Eduardo Jan 06 '13 at 07:24
  • in the shell, what is the output of **`type gnome-terminal`** in Ubuntu and the CentOS machines? – ggrandes Jan 06 '13 at 07:38

2 Answers2

0

Thanks for all of you. I have found the problem that is when I copy my program to CentOS the execution permission is missed so the terminal can't execute it and then dismiss.

Kery
  • 513
  • 8
  • 22
0
try{
    Process pr =new ProcessBuilder("gnome-terminal", "-e", "pathToScript/script.sh").start();
    }catch(Exception e){
        e.printStackTrace();
    }
naveen dahiya
  • 436
  • 5
  • 6