Reason for Incompatibleclasschangeerror

Asked

Viewed 205 times

2

I recently noticed some error messages in my code, the message that appears is this:

Exception in thread "Thread-3" java.lang.Incompatibleclasschangeerror

The strange thing is that it only happens with the jar ready and in other machines, in the machine where I generated the jar works normally.

What is the reason for this exception and how can I prevent it from happening?

1 answer

3


A mistake of the kind IncompatibleClassChangeError is usually the effect of an inappropriate compilation process.

For example:

  1. Create the following classes:

    public class Teste {
        public static void main(String[] args) {
            Teste2.hello();
        }
    }
    
    public class Teste2 {
        public static void hello() {
            System.out.println("Oi");
        }
    }
    
  2. Compile the classes like this:

    javac Teste.java Teste2.java
    
  3. Change the class Teste2, removing the modifier static:

    public class Teste2 {
        public void hello() {
            System.out.println("Oi");
        }
    }
    
  4. Recompile only the class Teste2:

    javac Teste2.java
    
  5. Run the program:

    java Teste
    

Here’s the way out:

Exception in thread "main" java.lang.IncompatibleClassChangeError: Expected static method Teste2.hello()V
        at Teste.main(Teste.java:3)

There are several other ways to generate a IncompatibleClassChangeError, but they are all the fruit of mixing classes that have been altered and recompiled in ways incompatible with each other. The best suggestion to fix this is to delete any files .class of your project and recompiling everything again from scratch.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.