java executing linux command

痴心易碎 提交于 2019-12-06 08:22:34

问题


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 ?


回答1:


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());
}



回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/9993873/java-executing-linux-command

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!