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?
3 Answers 3
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()
2 Comments
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);
Comments
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");