20

I have a strange problem with Eclipse Galileo.
I set Java 1.6 as my JRE. On this line of code

List templates = new ArrayList ();

I see the following error in Eclipse's problem list:

The type Collection is not generic; it cannot be parameterized with arguments

I don't have any problems with building this project with Ant.
How can I fix it? Looks like it is an Eclipse problem, but because of this error, I can't compile/publish my project from the IDE.

Pops
  • 30,199
  • 37
  • 136
  • 151
dbf
  • 415
  • 2
  • 10
  • 18
  • 1
    is your error actually at that line number? What line is the error on if you do something like: List templates; new ArrayList(); Don't do the assignment on the second line, just let me know what happens. It looks like you have some custom Collection class from that error that isnt of the signature: Collection... which doesn't make sense. – Matt Wonlaw Nov 26 '09 at 16:04

13 Answers13

38

What List are you importing? (see this thread from 2006)

java.awt.List or java.util.List?

Because, as eclipse aptly comments, java.awt.List is not parameterized ;)


Check also the

  • Java Build path: it must not contain a reference to the J2SE 1.4.2 libraries.
  • Source Compatibility: project properties -> Java Compiler Settings, Source Compatibility 5.0 or 6.0.

Other than that, there was lots of issue back in 2005 when the latest Eclipse 3.1 beta was supporting J2SE5, but this was fixed since then.

Try tyo use the latest JDK6 in your project.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    good catch. One of my favorites (when I don't pay attention) - on the other hand - it builds on ant and doesn't complain about List but about Collection. – Andreas Dolk Nov 26 '09 at 15:56
21

It's late but still replying, might be helpful for others who are still facing the issue. I was getting exactly the same issue. The List was proper with util.List. The solution was to order the exports of the libraries. If you are using Maven or any other Libraries :

In Project -> Build Path -> Configure Build Path -> Order & Exports

Check 'JRE System Libraries' should be above 'Maven Dependencies'

This worked for me.

Swagatika
  • 3,376
  • 6
  • 30
  • 39
  • This worked for me too. The issue was that Eclipse Jetty (HTTP server) was bringing in an Apache Felix jar called osgi foundation which contained a repackaged java.util.List! – David B Aug 15 '13 at 11:39
  • OMG! This worked for me too. Saved huge time. Thanks. – Ahamed Dec 24 '13 at 08:46
6

For those, who will get there from Google: the problem was with cryptix library. When I removed it from java build path the project is compiled sucesfully.

dbf
  • 415
  • 2
  • 10
  • 18
3

Did you name your class list? i.e:

import java.util.*;

public class List {   // can't do this, name this something else.

    public static void main(String[] args) {
        List<Integer> l = new ArrayList<Integer>();

    }

}
Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
2

Hey, I removed the cryptic library and it didn't work. But then I put JRE System Library at the top, and it worked. Really weird.

  • 2
    Right click on project ->Java Build Path -> Order and Export move JRE System Library [jkd1.6] to the top. Worked for me. – Michael Z Jun 28 '12 at 04:23
  • 2
    Worked for me because of the following reason: The "import java.util.List;" was importing a List that pre dates generics. A way to find out is Ctrl + "left click" in Eclipse; if it opens List.class whose interface definition is not "public interface List extends Collection" then it is importing a List that does not know generics. – Michael Z Jun 28 '12 at 04:35
1

for example:

public class AClass<T extends Object>
{
 public HashMap<String, String> myMap;
}

if I write:

public class BClass
{

private AClass aClass = new AClass();

public void test()
{

aClass.get("test");
//return Object class

}

}

but if I write:

public class BClass
{

private AClass<?,?> aClass = new AClass<?,?>();

public void test()
{

aClass.get("test");
//return String class

}

}
zen0n
  • 347
  • 2
  • 6
  • 16
1

Sometimes it's an eclipse hiccup and eclipse -clean plus refreshing all projects helps.

Edit

Does it change anything when you replace your code with:

java.util.List templates = new java.util.ArrayList();

or even

java.util.List<Object> templates = new java.util.ArrayList<Object>();

?

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • I've already tried it. Also I tried to remove .metadata folder and recreate a workspace. – dbf Nov 26 '09 at 16:02
1

put the entry "JRE System Library..." at the top in project, properties, java build path, order and export

Fabio
  • 11
  • 1
1

I changed the import

   import javax.swing.text.html.HTMLDocument.Iterator;

to

   import java.util.Iterator;

then it worked for me

upog
  • 4,965
  • 8
  • 42
  • 81
1

use "import java.util.List"

instead of default import "import antlr.collections.List;"

and use the JRE5 or above for generic support of collection API....

xxxx
  • 11
  • 1
0

Try to remove import antlr.collections.List; and click Ctr+space to use java.util

Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
Kadiri
  • 288
  • 3
  • 9
0

Some ideas:

  • check the JRE library being used in your project (check the package explorer).
  • check the installed JREs in the eclipse settings (same as used by ant).
  • comment out the line just to check if it really is the error cause.
  • retype the whole line from scratch.
  • install a new (clean) version of eclipse, in a new folder (testing).
user85421
  • 28,957
  • 10
  • 64
  • 87
0

make java buildpath reference to greater than or equal to java 1.5

or you try to add the "import java.util.List" statement then you can see that

eclipse is saying it is conflicting with some other List type

for example it may be conflicting with com.lowagie.xx.xxx.List etc try to avoid these import

statements

Sankar R.K
  • 77
  • 2
  • 10