0

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?

Burkhard
  • 14,596
  • 22
  • 87
  • 108
  • You may want to take a look at this question: http://stackoverflow.com/questions/12716121/how-to-run-different-commands-from-java-code?rq=1 – Burkhard Aug 16 '13 at 10:28

2 Answers2

1

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;
Bilbo Baggins
  • 2,899
  • 10
  • 52
  • 77
  • 1
    Take a look at [ProcessBuilder](http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html) 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
0

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

Antoine Marques
  • 1,379
  • 1
  • 8
  • 16