• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Unix commands in Runtime.getRuntime().exec()

 
Greenhorn
Posts: 8
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I am using the above code from inside my java program to create a tar.gz file of the folder abcd which is 10GB in size.

I've never used this command before and would like to know if it is advisable to use it.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Runtime.exec() is fine, but the suggested replacement is ProcessBuilder. A lot of people still use exec() though, and I think it just calls PB under the hood.
 
Sheriff
Posts: 28416
102
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe the "tar" command is very commonly used in Unix, so I don't see any reason not to use it.
 
Sheriff
Posts: 22855
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would actually stop using an external process, and switch to using Java code. Apache Commons Compress has a class TarArchiveOutputStream which works similar to ZipOutputStream. Wrap a GZIPOutputStream in it, and simply add entries recursively using File.listFiles():
I've added that parameter path because in a recursive call, you need to append the folder to it. This is the only way I've found to maintain the full path of the Tar entries. For example, the following could be a recursive call chain:
tarFolder(out, new File("/usr/local/myfolder/abcd"), ""); -- to tar the /usr/local/myfolder/abcd folder.
tarFolder(out, subFolder, "efgh"); -- to tar sub folder efgh where subFolder represents this sub folder.
tarFolder(out, subFolder, "efgh/ijkl"); -- to tar sub folder ijkl of folder efgh where subFolder represents this sub folder.
pseudo code: tarFile(out, file, "efgh/ijkl/file.ext"); -- to tar file file.ext inside folder ijkl] where [tt]file represents this file.


If you continue with Runtime.exec or ProcessBuilder, you should have read When Runtime.exec() won't first.
 
reply
    Bookmark Topic Watch Topic
  • New Topic