1278

Anytime I have to re-import my projects into Eclipse (if I reinstalled Eclipse, or changed the location of the projects), almost all of my overridden methods are not formatted correctly, causing the error:

The method must override a superclass method

It may be noteworthy to mention this is with Android projects for whatever reason, the method argument values are not always populated, so I have to manually populate them myself. For instance:

list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

    //These arguments have their correct names
    public void onCreateContextMenu(ContextMenu menu, View v, 
                                    ContextMenuInfo menuInfo) {                 
    }

});

will be initially populated like this:

list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

    //This methods arguments were not automatically provided    
    public void onCreateContextMenu(ContextMenu arg1, View arg2,
                                    ContextMenuInfo arg3) {
    }

});

The odd thing is, if I remove my code, and have Eclipse automatically recreate the method, it uses the same argument names I already had, so I don't really know where the problem is, other then it auto-formatting the method for me.

This becomes quite a pain having to manually recreate ALL my overridden methods by hand. If anyone can explain why this happens or how to fix it. I would be very happy.

Maybe it is due to the way I am formatting the methods, which are inside an argument of another method?

Arsen Khachaturyan
  • 6,472
  • 4
  • 32
  • 36
Tim H
  • 13,263
  • 3
  • 15
  • 9
  • 1
    Please check this item, it explains the use of override. I believe this practice is very important for everything not only for this situation in particularly. http://stackoverflow.com/questions/94361/when-do-you-use-javas-override-annotation-and-why – oagostinho Aug 09 '12 at 10:17

13 Answers13

1441

Eclipse is defaulting to Java 1.5 and you have classes implementing interface methods (which in Java 1.6 can be annotated with @Override, but in Java 1.5 can only be applied to methods overriding a superclass method).

Go to your project/IDE preferences and set the Java compiler level to 1.6 and also make sure you select JRE 1.6 to execute your program from Eclipse.

Lii
  • 9,906
  • 6
  • 53
  • 73
alphazero
  • 26,728
  • 3
  • 28
  • 26
  • 35
    My project was set to 1.6, so I shot for the stars and set it to 1.5, rebuilt, then back to 1.6. Miraculously this resolved my issue. Thanks for pointing me in the right direction! – Michael Krauklis Feb 09 '11 at 20:43
  • 6
    having the same issue...tried the suggestion given in this answer but still not successful. :( :( – aditya_gaur Aug 25 '11 at 12:22
  • 6
    @MichaelKrauklis I'm 1.6 and I'm having this issue. I've set to 1.5 and cleaned it and set back to 1.6 and cleaned it again. Still not resolved. Any ideas? – Pacerier Feb 13 '12 at 22:42
  • 6
    Same problem here, in my case, I had not installed JDK 1.6, only the latest 1.7, which apparently doesn't work with Android. Installing the older 1.6 and then following these instructions, should work :) – NoBugs Jul 22 '12 at 02:31
  • If the above solutions did not work, you can simply delete the errors in the Problems view after setting the compiler to 1.6 – Vasile Jureschi Oct 14 '12 at 12:33
  • 4
    It should be mentioned (like in the comments to the other answers) a clean build followed by a restart is often required. – gsingh2011 May 23 '13 at 03:45
  • 2
    Just modify 1.5 to 1.6 in your YourProject->RightClick->Properties->Java Compiler->1.5, to 1.6. Apply and Yes to rebuild. – J4cK Oct 28 '13 at 17:23
  • 1
    Many thx, thought I was going mad (project was 1.6, but just like Michael setting it to 1.5 and back did fix this problem)! – Jacob van Lingen Jul 29 '14 at 08:40
  • Like Anand Sunderraman said this answer combined with the next answer here is the solution. – Matthis Kohli Jun 23 '15 at 08:19
  • FIXED. I couldn't reliably get rid of the problem until I configured Maven (instead of Eclipse) by ` org.apache.maven.plugins maven-compiler-plugin 3.5.1 1.6 1.6 UTF-8 ` – uvsmtid Feb 24 '16 at 05:19
  • Thanks. Setting Java Version in Project->Properties->Project Facets->Java from 1.5 to 1.6 helps too, to solve this problem. – phil Jan 30 '17 at 13:00
308

With Eclipse Galileo you go to Eclipse -> Preferences menu item, then select Java and Compiler in the dialog.

Now it still may show compiler compliance level at 1.6, yet you still see this problem. So now select the link "Configure Project Specific Settings..." and in there you'll see the project is set to 1.5, now change this to 1.6. You'll need to do this for all affected projects.

This byzantine menu / dialog interface is typical of Eclipse's poor UI design.

Paul
  • 5,308
  • 1
  • 17
  • 18
80

In case this happens to anyone else who tried both alphazero and Paul's method and still didn't work.

For me, eclipse somehow 'cached' the compile errors even after doing a Project > Clean...

I had to uncheck Project > Build Automatically, then do a Project > Clean, and then build again.

Also, when in doubt, try restarting Eclipse. This can fix a lot of awkward, unexplainable errors.

Eagle
  • 2,006
  • 19
  • 25
Rollin_s
  • 1,973
  • 2
  • 18
  • 18
41

To resolve this issue, Go to your Project properties -> Java compiler -> Select compiler compliance level to 1.6-> Apply.

Sathyajith Bhat
  • 19,739
  • 21
  • 90
  • 126
false9striker
  • 1,943
  • 3
  • 34
  • 38
22

The answer by Paul worked for me partially. I still had one error then. So, in addition to that, I also had to go to Properties-> Project Facets and there set the Java version from 1.5 to 1.6.

Maybe that helps.

ulrich
  • 1,251
  • 3
  • 12
  • 33
14

If nothing of the above helps, make sure you have a proper "Execution environment" selected, and not an "Alternate JRE".

To be found under:

Project -> Build Path -> Libraries

Select the JRE System Library and click Edit....

If "Alternate JRE ..." is selected, change it to a fitting "Execution Environment" like JavaSE-1.8 (jre1.8.0_60). No idea why, but this will solve it.

phil294
  • 8,501
  • 7
  • 55
  • 84
  • 2
    I selected an "Execution Environment" then pressed OK and the error was gone. Then I came back to this same dialog, and selected "Workspace default JRE (...)", which was my previous selection when I was getting the error from Eclipse, then pressed OK, and the error did not happened ?!? This must be an Eclipse UI bug or something like that (I am on Eclipse Neon 4.6.2) – mljrg Feb 22 '17 at 18:11
14

Guys in my case none of the solutions above worked.

I had to delete the files within the Project workspace:

  • .project
  • .classpath

And the folder:

  • .settings

Then I copied the ones from a similar project that was working before. This managed to fix my broken project.

Of course do not use this method before trying the previous alternatives!.

will824
  • 2,115
  • 4
  • 26
  • 28
11

This is my second time encounter this problem. first time according the alphazero's recommendation it worked. but in the second time I set to 1.6 it don't work it just like 'CACHE' this error after clean and rebuild.

Try to switch off 'Build Automatically' as Rollin_s said -> error still here!

So I removed the problem project (already set to 1.6) from Package Explorer and import it again -> it start a rebuild and no error this time

Hope this help someone

ob.yann
  • 236
  • 3
  • 6
11

In my case this problem happened when I imported a Maven project into Eclipse. To solve this, I added the following in pom.xml:

<properties>
   ...
   <maven.compiler.source>1.8</maven.compiler.source>
   <maven.compiler.target>1.8</maven.compiler.target>
</properties>

Then in the context menu of the project, go to "Maven -> Update Project ...", and press OK.

That's it. Hope this helps.

mljrg
  • 3,545
  • 1
  • 27
  • 42
  • Thanks, I guess this overrides parent pom configuration or something. (I ad the problem the other way around, no problem from eclipse, but maven's compiler threw me "must override etc.") – Asoub Apr 28 '17 at 13:05
10

Fixing must override a super class method error is not difficult, You just need to change Java source version to 1.6 because from Java 1.6 @Override annotation can be used along with interface method. In order to change source version to 1.6 follow below steps :

  1. Select Project , Right click , Properties
  2. Select Java Compiler and check the check box "Enable project specific settings"
  3. Now make Compiler compliance level to 1.6
  4. Apply changes
Sunil
  • 422
  • 5
  • 12
6

In my case, none the solutions above works. I have to checkout my source code to another folder. From eclipse select File > Switch workSpaces > Other... and then import code to the new workspaces. it works after that.

2

This happens when your maven project uses different Compiler Compliance level and Eclipse IDE uses different Compiler Compliance level. In order to fix this we need to change the Compiler Compliance level of Maven project to the level IDE uses.

1) To See Java Compiler Compliance level uses in Eclipse IDE

*) Window -> Preferences -> Compiler -> Compiler Compliance level : 1.8 (or 1.7, 1.6 ,, ect)

2) To Change Java Compiler Compliance level of Maven project

*) Go to "Project" -> "Properties" -> Select "Java Compiler" -> Change the Compiler Compliance level : 1.8 (or 1.7, 1.6 ,, ect)

HashanR
  • 164
  • 12
0

It's 2020 -

Project>Right Click>Java Compiler>Compiler Compliance Level> Change this to 1.8 [or latest level]

enter image description here