title: -XX:OnOutOfMemoryError="<cmd args>" not working in Windows?
body: <p>My goal is to execute a script (Windows batch file) when an java.lang.OutOfMemoryError is thrown, as per the option described in the <a href="http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html" rel="nofollow">Hotspot VM Options</a></p>
  
  <pre><code>-XX:OnOutOfMemoryError="&lt;cmd args&gt;; &lt;cmd args&gt;"
  </code></pre>
  
  <p>I cannot get a script, or any other process, to execute when a OutOfMemoryError is thrown.  First I tried launching cmd.exe and specifying the path to the script, I modified where the doublequotes went, used frontslashes instead of backslashes, used double-backslashes.  I've abandoned slashes and quotes altogether and tried simple .exes the live in the working directory or on the PATH.  Nothing works.</p>
  
  <p>I can't get any executable to even <em>attempt</em> to run.  By using <a href="http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx" rel="nofollow">ProcMon</a> I've determined that the JVM isn't even looking for an executable with the name I specify, much less trying to launch it.</p>
  
  <p>I know I have the option specified correctly in the JVM params because it complains if I deliberately insert a typo.  I'm trying things like the following:</p>
  
  <pre><code>java -XX:OnOutOfMemoryError=myprocess.exe com.mycompany.SimulateOome
  java -XX:OnOutOfMemoryError="myprocess.exe" com.mycompany.SimulateOome
  java "-XX:OnOutOfMemoryError=myprocess.exe" com.mycompany.SimulateOome
  </code></pre>
  
  <p>Which produces the expected stack trace to stdout/err.</p>
  
  <p>It could be my test class.  My current version simply throws java.lang.OutOfMemoryError.  I've also simulated a more organic OOME by allocating a large array.  Neither works.</p>
  
  <p>I'm using JDK 1.6.0_38 on Windows 2003/2008 but also tried JDK 1.7.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>The test class was at fault. Neither simply throwing the exception, nor a simple huge array allocation, will cause the OOME handler to fire, despite otherwise successfully throwing the OOME. See below for Perception's OutOfMem class that does trigger the OOME handler. After sorting that out, the syntax is easy, and the quotes can either surround the entire option, or just the part after the equals. For a Windows shell script, one can use: </p>
  
  <pre><code>java "-XX:OnOutOfMemoryError=c:\windows\system32\cmd.exe /c c:\scripts\my_oome_handler.bat" com.mycompany.MyClass
  </code></pre>
  
  <p>or </p>
  
  <pre><code>java -XX:OnOutOfMemoryError="c:\windows\system32\cmd.exe /c c:\scripts\my_oome_handler.bat" com.mycompany.MyClass
  </code></pre>