2013-05-05 40 views
1

我试图运行此代码失败,注:的ProcessBuilder与gunzip解不起作用

的gzip:/home/idob/workspace/DimesScheduler/*.gz:没有这样的文件或目录

的代码:

ProcessBuilder gunzipPB = new ProcessBuilder("gunzip", System.getProperty("user.dir") + File.separator + "*"); 
gunzipPB.inheritIO(); 
int gunzipProcessExitValue; 

try { 
     gunzipProcessExitValue = gunzipPB.start().waitFor(); 
    } catch (InterruptedException | IOException e) { 
     throw new RuntimeException("Service " + this.getClass().getSimpleName() + " could not finish creating WHOIS AS Prefix Table", e); 
    } 

logger.info("Finished unzipping radb and ripe files. Process exit value : {}", gunzipProcessExitValue); 

退出值为1

相同的命令在TE rminal工作得很好(文件存在)。

可能是什么问题?

谢谢。

编辑: java.nio.file.NoSuchFileException: /home/idob/workspace/DimesScheduler/*.gz

任何想法可能是这个问题:

尝试使用DirectoryStrem我得到这个异常后?这些文件确实存在。

的完整代码:

ProcessBuilder radbDownloadPB = new ProcessBuilder("wget", "-q", "ftp://ftp.radb.net /radb/dbase/*.db.gz"); 
ProcessBuilder ripeDownloadPB = new ProcessBuilder("wget", "-q", "ftp://ftp.ripe.net/ripe/dbase/split/ripe.db.route.gz"); 

    radbDownloadPB.inheritIO(); 
    ripeDownloadPB.inheritIO(); 

    try { 

     int radbProcessExitValue = radbDownloadPB.start().waitFor(); 
     logger.info("Finished downloading radb DB files. Process exit value : {}", radbProcessExitValue); 

     int ripeProcessExitValue = ripeDownloadPB.start().waitFor(); 
     logger.info("Finished downloading ripe DB file. Process exit value : {}", ripeProcessExitValue); 

     // Unzipping the db files - need to process each file separately since java can't do the globing of '*' 
     try (DirectoryStream<Path> zippedFilesStream = Files.newDirectoryStream(Paths.get(System.getProperty("user.dir"), "*.gz"))){ 
      for (Path zippedFilePath : zippedFilesStream) { 
       ProcessBuilder gunzipPB = new ProcessBuilder("gunzip", zippedFilePath.toString()); 
       gunzipPB.inheritIO(); 
       int gunzipProcessExitValue = gunzipPB.start().waitFor(); 
       logger.debug("Finished unzipping file {}. Process exit value : {}", zippedFilePath, gunzipProcessExitValue); 
      } 
     } 

     logger.info("Finished unzipping ripe and radb DB file"); 

    } catch (InterruptedException | IOException e) { 
     throw new RuntimeException("Service " + this.getClass().getSimpleName() + " could not finish creating WHOIS AS Prefix Table", e); 
    } 

谢谢...

回答

1

*.gzglob不受gunzip命令处理,但外壳。例如,shell会将gunzip *.gz翻译为gunzip a.gz b.gz。现在,当你通过java执行时,你必须调用bash来为你做globbing,或者扩展java中的glob,因为gzip不知道如何处理glob。

Java 7有new libraries这使扩展glob模式更容易。

+0

谢谢。 正如你所说我可以使用java.nio.DirectoryStream或者我可以运行如下: 新的ProcessBuilder(“/ bin/sh”,“-c”,“gunzip ./*”); – Ido 2013-05-06 07:57:57