maven中javac编译命令错误的解决方法 [英] Workaround for javac compilation order bug in maven

查看:130
本文介绍了maven中javac编译命令错误的解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了Java编译器中的一个错误,其中提交用于编译的文件的顺序可能导致代码无法编译。我已经深入研究代码,以找出可以重现问题的最少量代码,从而产生三个源文件(每个1级)。

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 three source files (1 class each).

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");
    }
}

通过在特定的javac参数中重现问题订购。简而言之,为了成功,必须始终在使用它的Program类之前编译Actions类,否则javac无法以理智的方式处理它:

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:

# 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

编译错误,一旦发生,总是相同的 - Actions枚举上的run方法找不到实例,即使它们都实现了具有该run方法的接口。

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.

Program.java:6: cannot find symbol
symbol  : method run(java.lang.String)
location: class problem.Actions
        Actions.SKIP.run("hello");

该错误似乎与这个在甲骨文网站上报道了
我在mac os x 10.7.2 x86_64上使用javac 1.6.0_29,但也在Linux上复制了它。

The bug seems related to this one reported on Oracle's site. I'm using javac 1.6.0_29, on mac os x 10.7.2 x86_64, but have also reproduced it on Linux.

这个问题变得很明显了我正在使用Maven构建,并且似乎没有任何控制编译的顺序。所以我正在寻找一种解决方法,要么强制maven以这样的顺序编译文件以避免这种编译器错误,要么摆弄编译器标志(或类似的东西)以避免它。问题出现在工作站和持续集成环境中,所以它必须全面工作。有什么建议吗?

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?

编辑:刚试过以下的解决方法,尽管只是将有问题的枚举分配给它实现的接口类型的变量,但令人惊讶地导致错误消失。

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.

public class Program {

    public static void main(String[] args) {
        ActionSpec a = Actions.SKIP;
        a.run("hello");
    }
}

仍对其他人的意见感兴趣。

Still interested in others opinions.

推荐答案

我玩过,发现添加简单的演员:

I played around, and found that adding simple cast:

public static void main(String[] args) {
    ((ActionSpec)Actions.SKIP).run("hello");
}

解决了这个问题。将此枚举作为方法参数作为接口传递也可以实现技巧

solves this problem. Passing this enum as method parameter as interface would also do the trick

这篇关于maven中javac编译命令错误的解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆