aspose file tools
The moose likes Java in General and the fly likes Unix commands in Runtime.getRuntime().exec() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Unix commands in Runtime.getRuntime().exec()" Watch "Unix commands in Runtime.getRuntime().exec()" New topic
Author

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

Rishidharan Somu
Greenhorn

Joined: Sep 23, 2007
Posts: 8



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.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16487
    
    2

I believe the "tar" command is very commonly used in Unix, so I don't see any reason not to use it.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19232

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.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Unix commands in Runtime.getRuntime().exec()
 
Similar Threads
problem in running dos command from java
just the trick I want to start from beginning by pressing the then appeared button
Launching a DOS batch file
Running commands using Runtime.getRuntime().exec() in Unix
separate JVM's