Java 8 ClassFormatException for interface with static methods, but only when using assertions

二次信任 提交于 2019-12-12 13:07:25

问题


I started to get ClassFormatExceptions I couldn't explain relating to interfaces with static methods. I pruned it down to this test case:

public interface ModifierTest
{
    public static final int DELTA = 10;

    public static int increment(int value)
    {
        assert value > 0; // Problem line
        return value + DELTA;
    }
}

public class ModifierExec
{
    public static void main(String[] args)
    {
        System.out.println(ModifierTest.class);
    }
}

Without the assertion in the increment() method, everything is fine. But with the assertion, I get an exception at run time (compilation is fine):

Exception in thread "main" java.lang.ClassFormatError: Illegal field modifiers in class ModifierTest: 0x1018
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at com.c4.camgen.ModifierExec.main(ModifierExec.java:7)

This is easy to work round, but I'm curious as to whether this is a bug in Java or correct (if strange) behaviour. I can't find any references to assertions affecting field modifiers.

Can anyone tell me what's going on? Running in Eclipse Kepler, under jdk 1.8.0_20.


回答1:


First of all, Eclipse has its own compiler so a possible bug is there, not in javac.

Now, Juno is an old version which predates Eclipse's support for Java 8 and earlier versions of Java did not allow static methods in the interface at all. This is where your report gets confusing.

Anyway, you should upgrade to Luna to work seamlessly with Java 8 in Eclipse.



来源:https://stackoverflow.com/questions/25771952/java-8-classformatexception-for-interface-with-static-methods-but-only-when-usi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!