2011-05-31 47 views
0

返回不同的结果为什么'ulimit -a'通过Runtime.exec()以不同的方式返回,直接在bash下运行它,感谢任何指针。为什么ulimit通过java Runtime.exec()

的Java: 打开文件(-n)65536

的bash-3.00 $的ulimit -a: 打开文件(N)256

public class TestUlimit { 
    public TestUlimit() throws IOException, InterruptedException { 
      Runtime runTime = Runtime.getRuntime(); 
      Process p = runTime.exec(new String[] { "bash", "-c", "ulimit -a" }); 
      InputStream in = p.getInputStream(); 
      InputStreamReader isr = new InputStreamReader(in); 
      BufferedReader br = new BufferedReader(isr); 
      String line = null; 
      System.out.println("Result of Process p = runTime.exec(new String[]  { \"bash\", \"-c\", \"ulimit -a\" });"); 
      while ((line = br.readLine()) != null) { 
        System.out.println(line); 
      } 
      p.waitFor(); 

      p = runTime.exec("ulimit -a"); 
      in = p.getInputStream(); 
      isr = new InputStreamReader(in); 
      br = new BufferedReader(isr); 
      System.out.println("Result of p = runTime.exec(\"ulimit -a\");"); 
      while ((line = br.readLine()) != null) { 
        System.out.println(line); 
      } 
      p.waitFor(); 
    } 
+0

几乎不是Java的问题。 – EJP 2011-05-31 03:02:39

+0

将绝对路径添加到ulimit以确保您正在执行正确的路径,例如/ bin/uname – 2011-05-31 07:55:57

回答

0

庆典执行它自己的ulimit命令?检查.profile,.bashrc等。

+1

或企业级系统/ etc/profile,可能是/var/etc/profile.local。祝你好运。 – shellter 2011-05-31 04:21:38

0

ulimit是一个shell内置函数,其默认值基于shell的配置。 Java可能使用除bash以外的默认shell。即使情况并非如此,也可能是您有一些设置,例如.profile,当您有命令行时调用该设置,但在编程运行shell时不会。

+0

Java不会通过shell执行,除非你告诉它,所以必须有一个ulimit程序。 – EJP 2011-05-31 04:55:28

+0

哦。我没有意识到这一点。 – Dan 2011-05-31 13:52:20

+0

我的机器上没有ulimit程序。 – 2011-06-13 12:34:44