executing commands on terminal in linux through java

Harshit Agarwal picture Harshit Agarwal · Nov 12, 2010 · Viewed 11k times · Source

I have created an standalone application in which i want that when the user clicks on the run button then the terminal should open and a particular command should be executed on the terminal. I am able to open the terminal successfully using the following code...

Process process = null;  
try {  
    process = new ProcessBuilder("xterm").start();  
} catch (IOException ex) {  
    System.err.println(ex);  
}  

The above code opens a terminal window but I am not able to execute any command on it. Can anyone tell me how to do that?

Answer

Kilian Foth picture Kilian Foth · Nov 12, 2010

Try

new ProcessBuilder("xterm", "-e", 
                   "/full/path/to/your/program").start()