3

I`m trying to execute linux commant 'cat' from java code, but it does not working.

Runtime.getRuntime().exec("cat /home/roman/logs/*");  

And it working well for cat of single file

Runtime.getRuntime().exec("cat /home/roman/logs/mylog.log");

My question is how to cat all files on some dir from java ?

Roman Iuvshin
  • 1,872
  • 10
  • 24
  • 40
  • possible duplicate of [Want to invoke a linux shell command from Java](http://stackoverflow.com/questions/1410741/want-to-invoke-a-linux-shell-command-from-java) – MK. Apr 03 '12 at 13:05

3 Answers3

4

You could put all files under the dir into a collection and iterate over it:

File[] files = dir.listFiles();
for (File f : files) {
  Runtime.getRuntime().exec("cat "+dir.getAbsolutePath()+File.separator+f.getName());
}
Yuval
  • 7,987
  • 12
  • 40
  • 54
3

You can't use * with the exec() command (you would need a shell). A solution could be to write a script and then exec() that script from your java application.

talnicolas
  • 13,885
  • 7
  • 36
  • 56
2

Runtim.exec() does not use a shell to execute the command. Therefore the wildcard is not expanded. Try the solution explained in Want to invoke a linux shell command from java

Community
  • 1
  • 1
user1225148
  • 116
  • 2