0

I want a list of .tar.gz files from a specified directory. For this I am running "ls directory_path/*.tar.gz" using getRuntime.exec() in Java. But it is not giving any output rather it is saying No such file or directory.. But the same command is running on command prompt.. I am running java in unix. Thanks in advance

Luna
  • 956
  • 4
  • 15
  • 28
akhila
  • 35
  • 2
  • 7

2 Answers2

1

This is because exec won't launch a shell just to run your program. It just starts a process. On Unix-like systems the shell is responsible for expanding wildcards to lists of files. So you would need to run ls through a shell to get the desired behaviour.

However, why do you use ls at all? This answer shows how to get a list of files with Java.

Community
  • 1
  • 1
Joey
  • 344,408
  • 85
  • 689
  • 683
  • ok thanks.can you plz explain in some more detail and how to execute ls directorypath/*.tar.gz? – akhila Aug 03 '12 at 05:51
  • ok. I want to run many unix commands using java. How to run unix commands using java? – akhila Aug 03 '12 at 05:57
  • Just run them like you would run any other program? You already did so with `ls`, you know? – Joey Aug 03 '12 at 07:20
0

1. You will be able to fire processes using the exec() not the command promt..

2. Better use list() to get the names of all the files in a directory, and then use FileFilter to get the .tar.gz files.

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75