I want to create an application which provides GUI using a java swing but in that application i need to use terminal to execute the commands of Linux Ubuntu OS like sudo apt-get update and other.

Is there any method or code which executes my command in terminal but in background when I click on any button (on-click event) in form which is built using java swing?

share|improve this question
    
You may want to take a look at this question: stackoverflow.com/questions/12716121/… – Burkhard Aug 16 '13 at 10:28

You can also use ProcessBuilder class I used it one of my projects to execute commands in Linux(Fedora 18) to execute a particular process.

ProcessBuilder pb = new ProcessBuilder("ls");
Process p = pb.start();
p.waitFor();// This will wait untill the execution complets;
share|improve this answer
1  
Take a look at ProcessBuilder Java Docs for more details – MadProgrammer Aug 16 '13 at 10:39
    
Also use SwingWorker (or equivalent) to read the IO streams without blocking the EDT. – trashgod Aug 16 '13 at 17:18

You can use Runtime.exec() methods to run commands. This creates a Process. You can then waitFor for your process to finish, read it's output for command's results, etc ...
Please see javadoc of java.lang.Runtime.exec and java.lang.Process

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.