3

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.

JonMoore
  • 33
  • 3
  • It would be interesting to javap that class and see what's there. I'm guessing that *assert* generates some hidden field info and that's what's choking the verifier. – Hot Licks Sep 10 '14 at 18:11
  • Good thought. Haven't used javap for years. Eclipse adds "static final boolean $assertionsDisabled" to the class. Standard javac doesn't, so I'm guessing this will run fine through the Maven build. – JonMoore Sep 10 '14 at 19:51

1 Answers1

3

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.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436