-1

I am trying to create a process to invoke "ls *.xml".

This code works ok:

Process p=Runtime.getRuntime().exec("ls build.xml");
build.xml

This code is wrong:

Process p=Runtime.getRuntime().exec("ls *.xml");
// this is the output
Cannot run program "ls *.xml": error=2, No such file or directory

Any idea?

Smita Ahinave
  • 1,901
  • 7
  • 23
  • 42

1 Answers1

1

That's because wildcard expansion is done by Bash shell, not by ls command.

If all you want to do is list files, use Files.newDirectoryStream:

Files.newDirectoryStream(Paths.get(".")), p -> p.toString().endsWith(".xml")).forEach(System.out::println)
Alexey Soshin
  • 16,718
  • 2
  • 31
  • 40