Note that there are some explanatory texts on larger screens.

plurals
  1. POWorkaround for javac compilation order bug in maven
    text
    copied!<p>I'm encountering a bug in the Java compiler where the order of files submitted for compilation can cause code not to compile. I've drilled down the code to isolate the smallest amount of code I could to reproduce the problem, resulting in <a href="https://github.com/benhardy/java-compiler-bugs">three source files</a> (1 class each).</p> <pre><code>public interface ActionSpec { public abstract int run(String param); } public enum Actions implements ActionSpec { SKIP { public int run(String d) { return 0; } }; } public class Program { public static void main(String[] args) { Actions.SKIP.run("hello"); } } </code></pre> <p>The problem is reproducible by having javac arguments in a particular order. In short, in order to succeed, the Actions class must always be compiled before the Program class which uses it, otherwise javac just fails to deal with it in a sane way:</p> <pre><code># this case fails echo "Trying order: javac Program.java Actions.java ActionSpec.java" rm *class javac -verbose Program.java Actions.java ActionSpec.java # this case fails #rm *class #javac Program.java Actions.java ActionSpec.java # this case fails #rm *class #javac ActionSpec.java Program.java Actions.java # this case succeeds #rm *class #javac ActionSpec.java Actions.java Program.java # this case succeeds #rm *class #javac Actions.java ActionSpec.java Program.java # this case succeeds #rm *class #javac Actions.java Program.java ActionSpec.java </code></pre> <p>The compilation error, when it occurs, is always the same - the run method on the Actions enum instances cannot be found, even though they all implement an interface that has that run method.</p> <pre><code>Program.java:6: cannot find symbol symbol : method run(java.lang.String) location: class problem.Actions Actions.SKIP.run("hello"); </code></pre> <p>The bug seems related to <a href="http://bugs.sun.com/view_bug.do?bug_id=6199662">this one reported on Oracle's site</a>. I'm using javac 1.6.0_29, on mac os x 10.7.2 x86_64, but have also reproduced it on Linux.</p> <p>This problem became apparent as I am using Maven to build, and don't appear to have any control over the order of compilation. So I am looking for a workaround to either force maven to compile files in such an order as to avoid this compiler bug, or fiddle with the compiler flags (or something like it) to avoid it. The problem crops up on workstations and in continuous integration environments alike, so it would have to work across the board. Any suggestions?</p> <p>EDIT: Just tried the following workaround, which despite merely assigning the enum in question to a variable with the type of the interface it implements, surprisingly causes the error to disappear.</p> <pre><code>public class Program { public static void main(String[] args) { ActionSpec a = Actions.SKIP; a.run("hello"); } } </code></pre> <p>Still interested in others opinions.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload