8

I don't know why when I code:

List<String> data = new ArrayList<>();

it said that

diamond operator is not supported in -source 1.5
  (use -source 7 or higher to enable diamond operator)
----
(Alt-Enter shows hints)

I already use JDK 1.7. When I opened it in eclipse, I didn't get that error.

Radim
  • 4,721
  • 1
  • 22
  • 25
vuhoanghiep1993
  • 715
  • 1
  • 8
  • 15
  • 1
    Maybe you are using Maven? Java 1.5 is the default for Maven. You have to explicitely configure the compiler to use Java 7. – Seelenvirtuose Apr 08 '14 at 16:58
  • <> represents generics .. if u need java 1.5 stick with List a; if you can get 1.7 jump to List a; – Srinath Ganesh Apr 08 '14 at 17:22
  • My project had sub-projects, and while the main project used JDK 11 set. The source/binary format of the sub-projects were defaulted to 1.5. The fix for me was actually Opening the sub-projects, which then updated the source/binary format to match the parent project (JDK 11). – dko Feb 08 '21 at 15:59

3 Answers3

19

-source 1.5 means your code will be compatible with Java version 1.5 and cannot use language constructs introduced later. Read http://docs.oracle.com/javase/8/docs/technotes/tools/unix/javac.html to find more.

Easy way to achieve what you want (to be able to use diamond operator added in Java 7) is to update project source/binary version in project customizer - go to Projects tab (Ctrl-1), select project node, choose Properties in its context menu and update Source/Binary Format field in Source tab.

Radim
  • 4,721
  • 1
  • 22
  • 25
4

You missed this:

List<String> data = new ArrayList<String>();

You have to put "String" in both "<>"

Ale
  • 49
  • 1
4

right click and choose Properties follow this picture and select new version of source

Ruthe
  • 363
  • 4
  • 9