ramana
ramana

Reputation: 51

how to execute command from Java application?

How to execute Windows or Linux commands from a Java application?

I want to run dir command from Java application without a command prompt.

How can this be done?

Upvotes: 1

Views: 226

Answers (3)

pap
pap

Reputation: 27614

Just a note, dir is not an executable as such, but a command in the Windows command interpreter. To run dir, you would do Runtime.getRuntime().exec("cmd /C dir");

Upvotes: 0

Low Flying Pelican
Low Flying Pelican

Reputation: 6054

probably you may need to read output of executed app as well, if you need that

Process p = Runtime.getRuntime().exec("...");
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = br.readLine()) != null)
      System.out.println(line);

Upvotes: 0

Herbert Poul
Herbert Poul

Reputation: 4883

well .. you could do it with

Runtime.getRuntime().exec("...")

but you really don't want to ..

if you want to get a list of files in a directory use the File api! something like file.listFiles()

Upvotes: 7

Related Questions