Java running linux command
I am trying to execute linux commant 'cat' from java code but it doesn't work.
Runtime.getRuntime().exec("cat /home/roman/logs/*");
And it works well for a single file cat
Runtime.getRuntime().exec("cat /home/roman/logs/mylog.log");
My question is how to get all the files in some directory from java?
+3
Roman Iuvshin
source
to share
3 answers
You can put all files under a directory in a collection and loop over it:
File[] files = dir.listFiles();
for (File f : files) {
Runtime.getRuntime().exec("cat "+dir.getAbsolutePath()+File.separator+f.getName());
}
+4
Yuval
source
to share
You cannot use *
with a command exec()
(you need a shell). The solution could be to write a script and then exec()
which script from your java application.
+3
talnicolas
source
to share
Runtim.exec () does not use a shell to execute the command. Therefore, the wildcard is not expanded. Try the solution in Want to invoke linux shell command from java .
+2
user1225148
source
to share