Runtime.exec() and fork() and Process

So let me see if I understand this correctly, and if I don't please fill me in. If I call the following piece of codeRuntime.getRuntime().exec("chgrp rcsweb " + path+fs+orgName);
Runtime.getRuntime().exec("chmod 660 " + path+fs+orgName);where path is the real path to a file, fs is short for File.spearator and orgName is the name of a file, then in the Unix environment, it will fork off a process.
First, I seem to remember one forum topic that mentioned not forgetting about killing the process returned by exec, so I guess I need to add that to my code.
Second, since I am forking off this process inside of a J2EE container, am I actually forming off the entire container, or just the Servlet where Runtime is called, or what?
Third, how can I prevent the error java.io.IOException: Not enough space
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:54)
at java.lang.Runtime.execInternal(Native Method)
at java.lang.Runtime.exec(Runtime.java:546)
at java.lang.Runtime.exec(Runtime.java:413)
at java.lang.Runtime.exec(Runtime.java:356)
at java.lang.Runtime.exec(Runtime.java:320)when I do not have the option of changing my VM size? Is killing the Process (and using waitFor()) going to take care of this issue?
I appreciate your help.

OK, after writing a few test programs, it is indeed clear..on unix (at least on solaris) when calling runtime.exec(..) The ENTIRE VM does indeed get forked. Therefore if your current Memory allocation for you VM is sitting at say 64MB (or you initial memory setting), another 64MB process will be created, even if you doing something as a simple
exec("ls");
I also tried putting runtime.exec call in its own thread hoping that if it was not in the main thread, that only that process would be forked..to no avail..the entire vm is indeed forked everytime...making runtime.exec damn near useless for server side work, and very dangerious..as you can essentialy assume you always need 2x your memory requirements.
If anyone has figured out a workaround...any suggestions greatly appreciated.
Some thoughts on some work-arounds
1. Run 2 VMs (then when native calls are required..send a message to the 'native-calling vm' (insure its memory footprint is very low)
2. Write your exec method via JNI (or does JNI calls also result in a fork of the vm on unix..)
Test code below (threaded version)
import java.util.*;
import java.io.*;
public class test5 extends Thread
     public void run()
          int loops = 0;
          while (true)
               loops++;
               System.out.println("---- iteration " loops "---");
               makeNativeCall();
     public static void main(String[] args)
          try
               System.out.println("testing on OS: " + System.getProperty("os.name"));
               // Allocate a nice chunk of memory
               byte[] bytes = new byte[1024 * 1000 * 50];
               test5 tst = new test5();
               tst.start();
               while (true)
                    Thread.sleep(1000);
     catch(Exception e)
     {e.printStackTrace();}
public void makeNativeCall()
          Runtime runtime = Runtime.getRuntime();
InputStream stderr = null;
InputStream stdout = null;
          OutputStream stdin = null;
Process proc = null;
long totalMem = (long)(runtime.totalMemory()/1024);
long freeMem = (long)(runtime.freeMemory()/1024);
long usedMem = (long)((totalMem - freeMem));
System.out.println("VM REPORT: TOTAL(" totalMem") FREE("+freeMem+") USED(" usedMem")" );
try
proc = runtime.exec("sleep 1");
stderr = proc.getInputStream();
stdout = proc.getErrorStream();
stdin = proc.getOutputStream();
String line = null;
int i = 0;
String error = "";
int exitVal = proc.waitFor();
if (exitVal != 0)
System.out.println("ERROR " +exitVal);
System.out.println("DETIAL" +error);
catch(Exception e)
e.printStackTrace();
finally
if (stderr != null)
try{stderr.close();}catch(Exception ignore){ignore.printStackTrace();}
               if (stdout != null)
try{stdout.close();}catch(Exception ignore){ignore.printStackTrace();}
               if (stdin != null)
try{stdin.close();}catch(Exception ignore){ignore.printStackTrace();}
if (proc != null)
try{proc.destroy();}catch(Exception ignore){ignore.printStackTrace();}

Similar Messages

  • Using Runtime exec() to open and close process like java or javac

    Hi, I m a student who is learning IT and is wondering if
    Runtime exec() can run and stop commands like java and javac?
    Can someone post the code to do so here?Thank you

    Well, Here is my complete code:
    import java.util.*;
    import java.io.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    class StreamGobbler extends Thread
    InputStream is;
    String type;
    StreamGobbler(InputStream is, String type)
    this.is = is;
    this.type = type;
    public void run()
    try
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    String line=null;
    while ( (line = br.readLine()) != null)
    System.out.println(type + ">" + line);
    } catch (IOException ioe)
    ioe.printStackTrace();
    class Program implements Runnable
         Process proc;
    String args[];
         String[] cmd = new String[4];
         boolean isGone=false;
         public Program(String args[])
              this.args=args;
         public void run()
              if (isGone==true)
                   try
                        Runtime.getRuntime().exec(cmd).destroy();
                   catch(IOException e)
                        System.out.println("Error: "+e.toString());
                   System.out.println("User abort");
         public void start(String args[])
              if (args.length < 1)
    System.out.println("USAGE: java GoodWindowsExec <cmd>");
    System.exit(1);
    try
    String osName = System.getProperty("os.name" );
    if( osName.equals( "Windows NT" ) )
    cmd[0] = "cmd.exe" ;
    cmd[1] = "/C" ;
    cmd[3] = args[0];
    else if( osName.equals( "Windows 95" ) ||osName.equals( "Windows 98" ))
    cmd[0] = "command.com" ;
    cmd[1] = "/C" ;
    cmd[2] = args[0];
                        cmd[3]=args[1];
    Runtime rt = Runtime.getRuntime();
    // System.out.println("Execing " + cmd[0] + " " + cmd[1]
    // + " " + cmd[2]+" "+args[1]);                                    
    proc = rt.exec(cmd);
    // any error message?
    StreamGobbler errorGobbler = new
    StreamGobbler(proc.getErrorStream(), "ERROR");
    // any output?
    StreamGobbler outputGobbler = new
    StreamGobbler(proc.getInputStream(), "OUTPUT");
    // kick them off
    errorGobbler.start();
    outputGobbler.start();
    // any error???
    // int exitVal = proc.waitFor();
    //System.out.println("ExitValue: " + exitVal);
    } catch (Throwable t)
    t.printStackTrace();
         public void destroy()
              isGone=true;
    class test1 implements ActionListener
         String s[]={"c:\\jdk1.3\\bin\\java.exe","ContactProcessor1"};
         GUI g=new GUI();
              Program p=new Program(s);
         public test1()
              //didnt work
              g.getStart().addActionListener(this);
              g.getStop().addActionListener(this);
              g.show();
         public void actionPerformed(ActionEvent e)
              if (e.getSource()==g.getStart())
                   p.start(s);
                   p.run();
              if (e.getSource()==g.getStop())
                   p.destroy();
                   p.run();
         public static void main(String args[])
              test1 t = new test1();
    class GUI extends JFrame
         JButton start;
         JButton stop;
         public GUI()
              super();
              getContentPane().setLayout(new FlowLayout());
              start=new JButton("start");
              stop=new JButton("stop");
              getContentPane().add(start);
              getContentPane().add(stop);
              setSize(100,100);
         public JButton getStart()
              return start;
         public JButton getStop()
              return stop;
    }

  • Runtime.exec() - using cp and files with spaces on unix

    I'm using the Runtime.exec method to copy files in a unix environment. This works fine until there is a file where the name has spaces in it. I've read the article on the pitfalls of using runtime and how it breaks a string on white spaces and this is what is happening. I've also found another topic that was having the same problem, but they were using /usr/bin/dos2unix. I've tried putting quotes around the filename, but it still breaks on the first space. Any suggestions on how to get around this or another way of doing this would be greatly appreciated.
    An example of the os command string is:
    /usr/bin/cp /tmp/file with space.doc /docs
    Thanks!

    Hi!
    Well I dont have any Sun machine right here to try this but in windows It works great.
    Have you tried something like this ?
    import java.io.*;
    public class OSCopy {
        public static void main(String[] args) {
            try {
                String space = " ";
                String copycmd = "E:\\cp.cmd";
                String source = "E:\\File with space.txt";
                String destination = "E:\\tmp";
                String cmd = copycmd + space + "\"" + source + "\"" + space + destination;
                System.out.println("cmd: " + cmd);
                Runtime runtime = Runtime.getRuntime();
                Process copy = runtime.exec( cmd );
                BufferedReader reader = new BufferedReader( new InputStreamReader( copy.getInputStream()) );
                String line = null;
                while( (line = reader.readLine()) != null ) {
                    System.out.println( line );
            catch (Exception e) {
                e.printStackTrace();
    cp.cmd is a simple dos copy command
    copy %1 %2
    */good luck!

  • Runtime.Exec with cmd and problem in waitFor

    Hi
    I am running
    Process p1 = Runtime.getRuntime().exec("cmd /c start /min <some.exe> <args to exe>");
    then I am checking
    Int ExitVal = p1.waitFor;
    If (ExitVal != 0) and so on.
    However, the problem is that the p1.waitFor does not really block the thread. It returns 0 and the program continues even though the some.exe has still not completed its execution.
    My guess is that Java is giving the exitcode of cmd.exe and not some.exe.
    If this analysis is correct, I am stumped. How do I get the exit code of some.exe?
    If my analysis is wrong, please help me in the right direction.
    Regards
    Shreekar

    "Enables a user to start a separate window in Windows
    from the MS-DOS prompt."<p>
    Again, I read it afterwards. However, another funny thing is happening now. Initially my command was
    "cmd start /min (some.exe) (some args) > somefile.txt"
    (I know I missed to show the output redirection in my original post.)
    </p>
    <p>
    Then I removed "start /min", it is working as expected.
    Then I removed the output redirection and now it hangs !!!
    I put back the output redirection and it is working. Does the output redirection make it wait in some way and force it to exit gracefully?
    </p>
    Any ideas?

  • Runtime Exec on Solaris and wildcards characters

    Hello,
    I'm trying use exec in the following manner on Solaris 2.8 JDK1.3.1
    e.g. exec( "ls /tmp/*.sh" );
    When the string contains any sort of punctuation or wildcarding the command doesn't execute, usually fails with RC=2.
    exec( "ls /tmp/??.sh" ); fails
    exec( "ls /tmp/go.sh" ); works
    Any ideas or suggestions?
    thanks.

    If you haven't found the answer yet, I found it in another article. Basically, * and other wildcards are expanded by the shell so Runtime.getRuntime()exec("ls /tmp/*") won't work. You need to use the String[ ] form of exec(), i.e. Runtime.getRuntime()exec( new String[ ] {"sh", "-c", "ls /tmp/*"} )
    hope that helps.

  • Runtime.exec() then kill parent process, then you lose the System.out

    If I do this:
    in Proggie 1:
    Process proc = Runtime.getRuntime().exec("proggie");
    System.exit();in Proggie 2:
    System.out.println("some string"); // goes nowhere??How can I redirect Proggie 2 System.out to the console that was used to start Proggie 1?

    How can I redirect Proggie 2 System.out to the
    console that was used to start Proggie 1?No way with current Java. This is a long and sad story that Java does not have i/o redirection. See for example one of the entries in the bug database:
    http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4960438
    Harald.
    http://www.ebi.ac.uk/Rebholz-srv/whatizit/software

  • T2K/T5K type of Solaris servers and Runtime.exec

    It is being consistently observed that on T2k/T5k type of Solaris servers (e.g. T5220), the UNIX commands sent from Java using the Runtime class APIs are not being reveived by Solaris. It does not happen always, only occasionally. But when it happens, the java process hang at process.waitFor(). We are using Java1.6.
    I read the Java World article "When Runtime.exec() won't" and the behavior we are seeing is not because of any of the known pitfalls mentioned in the article. Also the warning in the Runtime API that speaks about the limited buffer size is also not applicable in this case because we do see it working sometimes. All of a sudden Runtime.exec gets stuck. We have also observed that the problem is not dependent on the command we pass through the Runtime.exec API. It happens for any trivial command.
    For more information on the issue, when we execute a 'pstack' on the process id, we see the following output most of the time and the 'truss' output shows the java process to be in a sleeping state:
    # pstack 20650
    ff2c52d8 lwp_park (0, 0, 0)
    ff2bf350 cond_wait_queue (24dd90, 24dd78, 0, 0, 0, 0) + 28
    ff2bf770 cond_wait_common (24dd90, 24dd78, 0, 0, 0, 0) + 294
    ff2bf874 cond_wait (24dd90, 24dd78, 0, 0, 0, 0) + 10
    ff2bf8b0 pthread_cond_wait (24dd90, 24dd78, ff2f3700, 0, f337da00, ecf00000) + 8
    eedddacc kernel_delete_session (24dd60, 24dd58, 0, 1, 3, 1c63c) + 114
    eeddd7dc kernel_delete_all_sessions (0, 1, 0, 1c8b4, 0, eedfa000) + 98
    eedd1eb4 cleanup_library (0, 1, 0, eedfa000, 28180, 3) + 3c
    eedd1f1c kernel_fini (0, 1, 0, eedfa000, 28120, eedfa4a4) + 44
    eede89e0 _fini (ff3f40fc, ff3f5a70, 2b3f4, 0, ff3f4910, 821) + 4
    ff3c54b4 call_fini (ff3f40fc, f43518f0, eede89dc, ff3f42f0, ff3f42a8, ff3f4910) + cc
    ff3cfdc0 remove_hdl (f43518f0, f1c4e2bc, 0, 4000, ff300a54, 4821) + ac8
    ff3ca408 dlclose_intn (f4351978, ff3f4910, ff3f40fc, 2a520, ff3ca4bc, 0) + 24
    ff3ca4e8 dlclose (f4351978, 0, f4350b88, 0, 1c00, 1) + 24
    eee33904 pkcs11_slottable_delete (1, f9e2c0, 46ec90, eee46bb0, 0, 1) + 138
    eee2e42c pkcs11_fini (eee46b8c, 1, eee2e074, eee46000, 17c18, eee46b84) + 4c
    ff241f38 postforkchild_handler (1d18, ff2f3700, 1c00, 4, f337da00, ff2f3700) + 30
    ff2b74bc fork (0, 41, 0, f1c4e744, ff2f3700, f337da00) + 144
    fe8a8ee4 Java_java_lang_UNIXProcess_forkAndExec (ffffffff, 0, fee0c0, fe8c4000, 4, 2) + 6d0
    f940bc20 * java/lang/UNIXProcess.forkAndExec([B[BI[BI[BZLjava/io/FileDescriptor;Ljava/io/FileDescriptor;Ljava/io/FileDescriptor;)I+-20552
    f940bbc4 * java/lang/UNIXProcess.forkAndExec([B[BI[BI[BZLjava/io/FileDescriptor;Ljava/io/FileDescriptor;Ljava/io/FileDescriptor;)I+0
    f94058b8 * java/lang/UNIXProcess.<init>([B[BI[BI[BZ)V+62
    f9405764 * java/lang/ProcessImpl.start([Ljava/lang/String;Ljava/util/Map;Ljava/lang/String;Z)Ljava/lang/Process;+182
    f9405874 * java/lang/ProcessBuilder.start()Ljava/lang/Process;+112
    f9405874 * java/lang/Runtime.exec([Ljava/lang/String;[Ljava/lang/String;Ljava/io/File;)Ljava/lang/Process;+16                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

    If you haven't found the answer yet, I found it in another article. Basically, * and other wildcards are expanded by the shell so Runtime.getRuntime()exec("ls /tmp/*") won't work. You need to use the String[ ] form of exec(), i.e. Runtime.getRuntime()exec( new String[ ] {"sh", "-c", "ls /tmp/*"} )
    hope that helps.

  • Runtime.exec() returns exitValue wrongly on windows XP and Windows 200

    I am trying to execute a batch script from java program using runtime.exec(). I then process exit code for any failures. I see that exit code is always 0 in case of windows XP and Windows 2000. But the same code and script when executed on windows 2003 or unix gives appropriate exit code. Is there any issue with Windows XP and Windows 2000?
    Thanks,
    Sandeep

    I am trying to execute a batch script from java program using runtime.exec(). I then process exit code for any failures. I see that exit code is always 0 in case of windows XP and Windows 2000. But the same code and script when executed on windows 2003 or unix gives appropriate exit code. Is there any issue with Windows XP and Windows 2000?
    Thanks,
    Sandeep

  • Process proc=runtime.exec("sh","-c","//home//usr//mkdir abcd") not working

    my servlet calling the above line is not executing to make a directory
    "abcd" at specified location. i am using Mandrake Linux and Tomcat... can anybody expalin the error.

    Hi raghutv,
    I also have this error
    I use Tomcat 4.0 and Window Me
    Do u think that the problem is "Window OS" or Tomcat Setting
    I have a problem. I want to complie the other Java Program "hi.java" using servlet.
    I am compiling the servlet code succesfully, but the web page can't display anything.
    The real path of "hi.java" is in the
    D:\Program Files\Apache Tomcat 4.0\FYP\WEB-INF\classes\hi.java
    This is my servlet code..............pls help.............thx!
    /*********************** Hello.java *********************/
    import java.io.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    public class Hello extends HttpServlet
    public void service(HttpServletRequest req,
    HttpServletResponse res)
    throws ServletException, IOException
    try {
    String path = getServletContext().getRealPath("\\WEB-INF\\classes\\hi.java");
    Runtime runtime = Runtime.getRuntime();
    Process process = runtime.exec("javac.exe " + path);
    BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
    PrintWriter com_out = res.getWriter();
    com_out.println("<pre>");
    String line = null;
    while((line = br.readLine()) != null){
    com_out.println(line);
    //String[] cmd = { "ping.exe", "time.nist.gov" }; // Command and argument
    //String[] cmd = { "javac.exe","hi.java"}; // Command and argument
    //Process process = Runtime.getRuntime().exec(cmd); // Execute command
    //Process process = Runtime.exec(cmd); // Execute command
    //process.waitFor(); // Wait until process has terminated
    catch (Exception e) {
    System.err.println(e);

  • Runtime exec IOException issues

    Hello,
    I have spent two 10 hours days on this problem with countless search permutations and peering into everything from kernel source to java source. Unfortunately, I am not coming up with useful results.
    Overview:
    I have a very large java application (Apache solr) running on a server with 12GB memory. Without going into too much detail, this application at a point will use a Runtime exec call to fire off a bash shell script. Over time, however, I end up with an IOException "Cannot allocate memory." (More info and stack trace below.)
    That application aside I have been able to reproduce this by varying the -Xms and -Xmx jvm parameters with confusing results using the simple program below:
    //Use with various -Xms and -Xmx arguments to produce
    //IOException on call to Runtime.exec()
    import java.io.*;
    public class DoRuntime {
       public static void main(String args[]) throws IOException {
          Runtime runtime = Runtime.getRuntime();
          long total = runtime.totalMemory();
          long max = runtime.maxMemory();
          long free = runtime.freeMemory();
          System.out.println("total: " + total);
          System.out.println("max: " + max);
          System.out.println("free: " + free);
          Process process = runtime.exec("/bin/ls");
          InputStream is = process.getInputStream();
          InputStreamReader isr = new InputStreamReader(is);
          BufferedReader br = new BufferedReader(isr);
          String line;
          while ((line = br.readLine()) != null) {
             System.out.println(line);
    }Here are some sample runs:
    RUN1
    rwoodrum@util1wad:~/tmp$ java DoRuntime
    total: 188743680
    max: 954466304
    free: 187759312
    DoRuntime.class
    DoRuntime.java
    rwoodrum@util1wad:~/tmp$
    RUN2
    rwoodrum@util1wad:~/tmp$ java -Xms10g -Xmx10g DoRuntime
    total: 10290069504
    max: 10290069504
    free: 10236381080
    Exception in thread "main" java.io.IOException: java.io.IOException: Cannot allocate memory
            at java.lang.UNIXProcess.<init>(UNIXProcess.java:148)
            at java.lang.ProcessImpl.start(ProcessImpl.java:65)
            at java.lang.ProcessBuilder.start(ProcessBuilder.java:451)
            at java.lang.Runtime.exec(Runtime.java:591)
            at java.lang.Runtime.exec(Runtime.java:429)
            at java.lang.Runtime.exec(Runtime.java:326)
            at DoRuntime.main(DoRuntime.java:14)
    rwoodrum@util1wad:~/tmp$
    RUN3
    rwoodrum@util1wad:~/tmp$ java -Xms10g -Xmx11g DoRuntime
    total: 10290069504
    max: 10498867200
    free: 10236381080
    DoRuntime.class
    DoRuntime.java
    rwoodrum@util1wad:~/tmp$-----
    (FWIW, I get the same results replacing /bin/ls with something as lightweight as /bin/true.)
    As can be seen in the above output of RUN2, I have set a fixed heap size of 10GB. The jvm seems content with that heap size, but when it goes to exec the child process it becomes rather unhappy. From this I would perhaps conclude that ultimately inside the call to forkAndExec (libjava.so) the call to fork() failed with an ENOMEM. (As a side note, an strace on the process doesn't actually ever show a fork, only a call to clone - which utilizes a fork eventually - more on clone below.)
    The results of RUN3, however, seem to imply that the problem is within the jvm itself. In RUN3, I set the initial heap allocation to 10GB, as in RUN2, but I set the maximum at 11GB. One would think (or at least I would) that if the fork in RUN2 failed that it would certainly fail in the case of RUN3. It doesn't.
    The manpage of clone indicates that the child process will "share parts of its execution context with the calling process". Indeed, if I'm reading it correctly, it indicates that the child process stack (among other things) is housed in the parent process address space.
    Could it be that by setting this very large, fixed heap size as in RUN2, there is no room for the jvm to "maneuver" and properly handle the effort of the Runtime.exec? The fact that RUN3 is successful is what has made me think something like this, but I am by far an expert on how the jvm would handle that sort of thing.
    In our production setup of this application, I adjusted the Xmx setting to be something larger than the Xms setting. This worked for a period of time, but eventually produced the same results. I suspect over time the heap simply increased toward the max and again the jvm couldn't do its thing when it came time to RunTime.exec. The fact that this happens on the trivial Runtime.exec program would seem to rule out a memory leak of the production application.
    Ultimately something fishy is going on with memory, but with seemingly contradictory results, I am at a loss of where the problem lies.
    If I have omittied any potentially relevant information, please let me know. Any thoughts are greatly appreciated.
    Some basic system information:
    rwoodrum@util1wad:~/tmp$ free
                 total       used       free     shared    buffers     cached
    Mem:      12304936   12200376     104560          0     337276    7082508
    -/+ buffers/cache:    4780592    7524344
    Swap:      2097144         32    2097112
    rwoodrum@util1wad:~/tmp$ uname -a
    Linux util1wad 2.6.18-4-amd64 #1 SMP Mon Mar 26 11:36:53 CEST 2007 x86_64 GNU/Linux
    rwoodrum@util1wad:~/tmp$ java -version
    java version "1.5.0_10"
    Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_10-b03)
    Java HotSpot(TM) 64-Bit Server VM (build 1.5.0_10-b03, mixed mode)-ryan woodrum

    I've continued to investigate this with my discoveries below. I've found some more interesting nuances, but am still not satisfied with the explanation of what is going on here. For time reasons, I've capitulated to a somewhat lame workaround.
    The test run in the 1.6.0 jvm environment is somewhat revealing in that it confirms that the eventual call to fork (I'm kind of guessing it's the fork call here) is returning an ENOMEM. The 1.5.0 jvm was not returning the actual errno from the call in the JNI code. Checking out $linux_src/include/asm-generic/errno-base.h indeed enumerates ENOMEM to 12.
    When I saw this, I started looking at the memory commit inside of /proc/meminfo to see what was going on when I would launch the jvm with the varying Xmx and Xms parameters. Somewhat unsurprisingly at this point, when I would run with large a large heap, the Committed_AS would skyrocket by a proportion roughly equal to the fixed heap size. (See here http://lwn.net/Articles/28345/ for an definition of Committed_AS and other /proc/meminfo fields.)
    With the above information in hand, I wanted to confirm without a doubt that the kernel was the thorn in my side. So on a test system I started to play around with the vm_overcommit feature - see $linux_src/Documentation/vm/overcommit-accounting for definitions of modes 0,1, and 2). Sure enough, toying with these modes I was able to alter the behavior of launching the jvm. I switched from the default heuristic mode (0) to an "always overcommit" mode and it would launch and sucessfully fork and exec every time. No surprise there.
    What IS surprising, however, is that if, under the default heuristic mode, I specify the jvm parameters differently it will sucessfully run and fork and exec the subprocess. It seems odd to me that at the OS level, specifying `java -Xms4g -Xmx4g` will cause the subprocess fork to fail but specifying `java -Xms4g -Xmx5g` will NOT cause the fork to fail. Running with the latter parameters shows the exact same Committed_AS spike. This all begs the question(s): What's the difference? With a min heap size set to 4g (or whatever) on the both jvm runs, how does a jvm parameter (especially one that allows MORE growth of the heap) impact this call to fork? One variable in the picture at this point that I'm unsure of is the strace call to clone(). I don't know what this could be doing to make things behave differently.
    So as I noted, above, I have a relatively lame workaround. Blindly add more swap to increase the CommittedLimit inside the kernel. It'll never get used, it's a waste of space, and, perhaps most importantly, on principal it just rubs me the wrong way.
    -ryan woodrum

  • Java's Runtime.exec() method

    When you shell out to java's Runtime.exec() method, are the process name and arguments the same for the child process that is spawned.
    We see duplicated processes about the time when our logs tell us this command was run. However, we cannot seem to reproduce this. Has anyone else seen anyone this before?

    That's what I though too. But check this out . . .
    Our code looks as follows:
    private Runtime rt;
    private Process p;
    rt = Rutime.getRuntime();
    p = rt.exec(command);
    This exec() call creates a new process, which is a child of the java process that runs this command. The final process looks like the "command" string that is passed to the exec() method call. In our case, the command is a call to the /usr/bin/mail utility to send out faxes and emails.
    We ran a very tight loop executing the rt.exec() call over and over. What we found was that for a minor fraction of a second, the newly created process looks just like the original process including the same arguments. However, the PID's indicated that one process was the child of the other. This is why it looked like we had 2 of the same processes. WILD!
    Thanks guys!

  • Runtime.exec() fails

    Hi,
    I have a process with many executables which work together to copy a script to another machine, execute it and get back the results. I am using Runtime.exec to execute the process. The files are stored in the directory "/opt/mx/myDir".
    Below are two scenarios. One works and the other doesnt.
    1. Working case.
    #cd /opt/mx/myDir
    #java myJavaClass.
    2. Failing case.
    #java myJavaClass.
    The issue is that myJavaClass is part of another project and hence I cannot "cd" to /opt/mx/myDir. So it executes at some directory and fails.
    Hence I tried ProcessBuilder.directory(file) - This fails too.
    I have no logs or anything as the other process is not mine.
    Any help on debugging this will be great.
    Thanks.

    Apologies if this is too obvious, but is your command
    #java myJavaClass.
    or
    java myJavaClass
    The trailing period is wrong.

  • Java RunTime.exec in a JFrame?

    Dear all,
    Is it possible to run an external program (such as open an PDF file, or executing a program from Windows Command Prompt) by Runtime.exec(open.bat), and show the pdf file (or Command Prompt windows) inside a JFrame or JPanel ?
    Thanks!

    I don't know of any solution for you.
    But talk to Adobe instead, maybe they have something which you can use to render PDF-files in java.

  • Runtime.exec on Mac

    How to use runtime.exec on Mac. I need to start iCal and I don't know how. calling runtime.exec("iCal") , runtime.exec("/Applications/iCal") and runtime.exec("/Applications/iCal.app") throws error. Please help. I don't know anything about Mac.

    Yes I did the second way and it gives a runtime
    error. I wrote a program that accepts argument and
    then executes it using getRuntime().exec(argument);
    I am still not able to start any programs under Mac.
    The problem is that I don't know anything about Mac.Suggestion.
    Figure out how to launch it from the terminal. That should give you an idea of what the command should be I think.
    Sorry I can't remember OTOH how to get into the terminal but I know you can.

  • How to capture output from Runtime.exec() ?

    Hi,
    Well, the question is in the subject ...
    I'd like to capture the output of a process ran by Runtime.exec() in order to process it.
    thanks,
    ionel

    Okay ...
    Sorry for the post !
    I found the solution : Runtime.exec().getOutputStream()
    Thanks however
    ionel

Maybe you are looking for

  • How can i create a new calendar?

    how can i create a new calendar and change the color of it? ipod 5 generation

  • Where can I get an older version of iphoto for mac osx 10.4.11 on a PPC G4 processor

    I have an old Imac G4 and wish to upgrade it to it's maximum, it is running mac OSX 10.4.11 tiger and currently has iphoto 2. I have the installers for iphoto 4.0.3 and 6.0.6, but I cannot activate either of them because I only have iphoto 2.

  • Photos not staying after rendering

    I had this problem before, after installing Leopard. Got the machine fixed and doubled the RAM. Now I have a video of the holidays to edit and it STILL won't keep photos. They render and the panel turns black, the video stops when it gets to the phot

  • How to add constructors

    here is the program I have: public class Freshman      private String name;      private int score; // constructor with no parameter public Freshman () {      score = 0; // constructor with one parameter public Freshman (int newScore) {      score =

  • Animation chart not display in mac firefox

    i am viewing a stock index chart with firefox ver26.0 in mac and it is not showing up at all... but the stock index chart will show up with safari in mac without any issue... any idea why there is issue with firefox but not with safari?... i hope you