2015-10-30 67 views
-1

我试图在Mac上运行Java上的“tar”命令。我注意到这个命令被卡住了。基本上,文件大小不会增长,命令也不会返回。但是,如果我运行较小的directiry,它工作正常。使用Java的Mac(Unix)上运行命令 - 卡住

这里是我的代码:

try 
     { 
      Runtime rt = Runtime.getRuntime(); 

      Process process = new ProcessBuilder(new String[]{"tar","-cvzf",compressFileName+" "+all_dirs}).start(); 

      InputStream stdin2 = process.getInputStream(); 
      InputStreamReader isr2 = new InputStreamReader(stdin2); 
      BufferedReader br2 = new BufferedReader(isr2); 
      String line2 = null; 
      System.out.println("<OUTPUT>"); 
      while ((line2 = br2.readLine()) != null) 
       System.out.println(line2); 
      System.out.println("</OUTPUT>"); 
      int exitVal3 = process.waitFor(); 
      System.out.println("Process exitValue .....: " + exitVal3); 
     } catch (Throwable t) 
      { 
      t.printStackTrace(); 
      } 

我也试过:

String tile_command="tar -cvzf file.tar.gz dire_to_compress "; 
     String[] tile_command_arr= new String[]{"bash","-c",tile_command}; 

try 
     { 
      Runtime rt = Runtime.getRuntime(); 

      Process proc2 = rt.exec(tile_command_arr); 
      InputStream stdin2 = process.getInputStream(); 
      InputStreamReader isr2 = new InputStreamReader(stdin2); 
      BufferedReader br2 = new BufferedReader(isr2); 
      String line2 = null; 
      System.out.println("<OUTPUT>"); 
      while ((line2 = br2.readLine()) != null) 
       System.out.println(line2); 
      System.out.println("</OUTPUT>"); 
      int exitVal3 = process.waitFor(); 
      System.out.println("Process exitValue for tiling .....: " + exitVal3); 
     } catch (Throwable t) 
      { 
      t.printStackTrace(); 
      } 
+0

目录有多大?您的JVM仅限于多少内存? – alfasin

+0

对于任何目录,这看起来好像根本不应该工作。你确定你获得了*任何*输入的零退出值吗? – RealSkeptic

+0

目录大小为10GB ...我运行Java时未指定内存参数 – user836026

回答

2
ProcessBuilder(new String[]{"tar","-cvzf",compressFileName+" "+all_dirs}) 

问题就更为突出。

使用ProcessBuilder不能将两个参数与空间放在一起,并期望底层进程获得两个参数。它会得到一个,就像你运行该命令

tar -cvzf 'compressFileName all_dirs' 

这将有焦油奇怪,为什么你有一个非常时髦的文件名创建compressFileName(space)all_dirs,和你在哪里希望把它的内容是什么?

你需要的东西更接近

String[]{"tar", "-cvzf", compressFileName, all_dirs}; 

,或者如果all_dirs是多个目录,你需要将它们同时添加到字符串数组一个(使用字符串的ArrayList,然后拉动阵列在ArrayList之外)。

+0

顺便说一句,你不需要说“new String [] {...}”:只需将每个字符串作为单独的参数传递,因为构造函数需要可变参数。 –