<p>I'm trying to compile over 100 java classes from different packages from a clean
directory (no incremental compiles) using the following ant tasks:</p>

<pre><code>&lt;target name="-main-src-depend"&gt;
    &lt;depend srcdir="${src.dir}" 
            destdir="${bin.dir}" 
            cache="${cache.dir}"
            closure="true"/&gt;
&lt;/target&gt;   

&lt;target name="compile" depends="-main-src-depend"
        description="Compiles the project."&gt;

    &lt;echo&gt;Compiling&lt;/echo&gt;

    &lt;javac  target="${javac.target}"
            source="${javac.source}"
            debug="${javac.debug}"
            srcdir="${src.dir}"
            destdir="${bin.dir}"&gt;
        &lt;classpath&gt;
            &lt;path refid="runtime.classpath"/&gt;
            &lt;path refid="compile.classpath"/&gt;
        &lt;/classpath&gt;
    &lt;/javac&gt;
&lt;/target&gt;
</code></pre>

<p>However, the first time I run the compile task I always get a StackOverflowException. If I run the task again the compiler does an incremental build and everything works fine. This is undesirable since we are using <a href="http://cruisecontrol.sourceforge.net/" rel="noreferrer">CruiseControl</a> to do an automatic daily build and this is causing false build failures.</p>

<p>As a quick-and-dirty solution I have created 2 separate tasks, compiling portions of the project in each. I really don't think this solution will hold as more classes are added in the future, and I don't want to be adding new compile tasks every time we hit the "compile limit".</p>

[{'answer_id': 16953, 'author': 'McDowell', 'author_id': 304, 'author_profile': 'https://Stackoverflow.com/users/304', 'pm_score': 1, 'selected': False, 'text': '<p>Does this happen when you run the javac command from the command line? You might want to try the <a href="http://ant.apache.org/manual/Tasks/javac.html" rel="nofollow noreferrer">fork</a> attribute.</p>\n'}, {'answer_id': 16955, 'author': 'jmanning2k', 'author_id': 1480, 'author_profile': 'https://Stackoverflow.com/users/1480', 'pm_score': 1, 'selected': False, 'text': '<p>Try adding some variation of these attributes to the <a href="http://ant.apache.org/manual/Tasks/javac.html" rel="nofollow noreferrer">Ant <code>javac</code> task</a> line:</p>\n\n<pre><code>memoryinitialsize="256M" memorymaximumsize="1024M"\n</code></pre>\n\n<p>You can also try <code>fork="true"</code>, not sure if this allows you to set values for stack and heap (aka -Xm1024), but it may help (if it would work from the command line, but not in Ant).</p>\n\n<p>[Edit]:\nAdded link -- the <a href="http://ant.apache.org/manual/Tasks/javac.html" rel="nofollow noreferrer"><code>javac</code> task</a> page would seem to suggest that the parameters above require that you do also set <code>fork="true"</code>.</p>\n'}, {'answer_id': 16982, 'author': 'Kieron', 'author_id': 588, 'author_profile': 'https://Stackoverflow.com/users/588', 'pm_score': 0, 'selected': False, 'text': '<p>That\'s quite odd, 100 classes really isn\'t that many. What is the compiler doing when the stack overflows? Is there a useful stack trace generated? What happens if you run <code>javac</code> directly on the command line instead of thorugh ant?</p>\n\n<p>One possible workaround is to simply increase the size of the stack using the <code>-Xss</code> argument to the JVM; either to the JVM running <code>ant</code> or by setting <code>fork="true"</code> and a <code>&lt;compilerarg&gt;</code> on the <code>&lt;javac&gt;</code> task. Actually now that I think of it, does the problem go away just putting in the <code>fork="true"</code>?</p>\n'}, {'answer_id': 18086, 'author': 'Elliot Vargas', 'author_id': 2024, 'author_profile': 'https://Stackoverflow.com/users/2024', 'pm_score': 0, 'selected': False, 'text': '<p>Here is what I found.\nAfter posting my question I went on and modified the compile task with the attributes <code>fork="true"</code>, <code>memoryinitialsize="256m"</code> and <code>memorymaximumsize="1024m"</code> (a found today that this was suggested by Kieron and jmanning2k, thanks for your time). This didn\'t solve the problem nonetheless.</p>\n\n<p>I decided to start removing classes from the source tree to see if a could pinpoint the problem. Turns out we had a Web Service client class for <a href="http://ws.apache.org/axis/" rel="nofollow noreferrer">Axis 1.4</a> that was auto-generated from a WSDL file. Now, this class is a monster (as in Frankenstein), it has 167 field members (all of them of type String), 167 getter/setter pairs (1 for each field), a constructor that receives all 167 fields as parameters, an equals method that compares all 167 fields in a strange way. For each field the comparison goes like this:</p>\n\n<pre><code>(this.A == null &amp;&amp; other.getA() == null) || (this.A != null &amp;&amp; this.A.equals(other.getA()))\n</code></pre>\n\n<p>The result of this comparison is "anded" (&amp;&amp;) with the result of the comparison of the next field, and so on. The class goes on with a hashCode method that also uses all fields, some custom XML serialization methods and a method that returns a Axis-specific metadata object that describes the class and that also uses all field members.</p>\n\n<p>This class is never modified, so I just put a compiled version in the application classpath and the project compiled without issues. </p>\n\n<p>Now, I know that removing this single source file solved the problem. However, I have absolutely no idea as to why this particular class caused the problem. It will be nice to know; what can cause or causes a StackOverflowError during compilation of Java code? I think I\'ll post that question.</p>\n\n<p>For those interested:</p>\n\n<ul>\n<li>Windows XP SP2</li>\n<li>SUN\'s JDK 1.4.2_17</li>\n<li>Ant 1.7.0</li>\n</ul>\n'}, {'answer_id': 19782, 'author': 'McDowell', 'author_id': 304, 'author_profile': 'https://Stackoverflow.com/users/304', 'pm_score': 4, 'selected': True, 'text': '<blockquote>\n  <p>It will be nice to know; what can\n  cause or causes a StackOverflowError\n  during compilation of Java code?</p>\n</blockquote>\n\n<p>It is probable that evaluating the long expression in your java file consumes lots of memory and because this is being done in conjunction with the compilation of other classes, the VM just runs out of stack space. Your generated class is perhaps pushing the legal limits for its contents. See chapter <a href="http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html#88659" rel="nofollow noreferrer">4.10 Limitations of the Java Virtual Machine</a> in <a href="http://java.sun.com/docs/books/jvms/" rel="nofollow noreferrer">The Java Virtual Machine Specification, Second Edition</a>.</p>\n\n<p><strong>Fix 1: refactor the class</strong></p>\n\n<p>Since your class is being generated, this might not be an option. Still, it is worth looking at the options your class generation tool offers to see if it can produce something less troublesome.</p>\n\n<p><strong>Fix 2: increase the stack size</strong></p>\n\n<p>I think <a href="https://stackoverflow.com/questions/16935/ants-javac-tasks-throws-stackoverflowexception#16982">Kieron</a> has one solution when he mentions the -Xss argument. <a href="http://java.sun.com/javase/6/docs/technotes/tools/windows/javac.html" rel="nofollow noreferrer">javac</a> takes a number of non-standard arguments that will vary between versions and compiler vendors.</p>\n\n<p>My compiler:</p>\n\n<pre><code>$ javac -version\njavac 1.6.0_05\n</code></pre>\n\n<p>To list all the options for it, I\'d use these commands:</p>\n\n<pre><code>javac -help\njavac -X\njavac -J-X\n</code></pre>\n\n<p>I <em>think</em> the stack limit for javac is 512Kb by default. You can increase the stack size for this compiler to 10Mb with this command:</p>\n\n<pre><code>javac -J-Xss10M Foo.java\n</code></pre>\n\n<p>You might be able to pass this in an Ant file with a <em>compilerarg</em> element nested in your <em>javac</em> task.</p>\n\n<pre><code>&lt;javac srcdir="gen" destdir="gen-bin" debug="on" fork="true"&gt;\n    &lt;compilerarg value="-J-Xss10M" /&gt;\n&lt;/javac&gt;\n</code></pre>\n'}, {'answer_id': 1042236, 'author': 'npellow', 'author_id': 2767300, 'author_profile': 'https://Stackoverflow.com/users/2767300', 'pm_score': 2, 'selected': False, 'text': '<pre><code>  &lt;javac srcdir="gen" destdir="gen-bin" debug="on" fork="true"&gt;\n      &lt;compilerarg value="-J-Xss10M" /&gt;\n    &lt;/javac&gt;\n</code></pre>\n\n<p>from the <a href="https://stackoverflow.com/questions/16935/ants-javac-tasks-throws-stackoverflowexception/19782#19782">comment above</a> is incorrect. You need a space between the -J and -X, like so:</p>\n\n<pre><code>&lt;javac srcdir="gen" destdir="gen-bin" debug="on" fork="true"&gt;\n    &lt;compilerarg value="-J -Xss10M" /&gt;\n&lt;/javac&gt;\n</code></pre>\n\n<p>to avoid the following error:</p>\n\n<pre><code> [javac] \n[javac] The \' characters around the executable and arguments are\n[javac] not part of the command.\n[javac] Files to be compiled:\n</code></pre>\n\n<p>...\n    [javac] javac: invalid flag: -J-Xss1m\n    [javac] Usage: javac  </p>\n'}, {'answer_id': 66958359, 'author': 'l k', 'author_id': 6292018, 'author_profile': 'https://Stackoverflow.com/users/6292018', 'pm_score': 0, 'selected': False, 'text': '<p>Some of the other answers mentioned fixes that require setting <code>fork=&quot;true&quot;</code>, but another option is to bump up the stack space of the underlying JVM created by ant, by setting the <code>ANT_OPTS</code> environment variable:</p>\n<pre class="lang-sh prettyprint-override"><code>ANT_OPTS=-Xss10M ant\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/16935', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2024/']