Diamond operator(<>) not working in java 1.7

2020-02-15 03:05发布

问题:

I have the following error for compiling a jsp file:

'<>' operator is not allowed for source level below 1.7

I'm using jdk 1.7.x and eclipse Kepler Also I have set 1.7 as compliance level in project preferences in Eclipse, still the code is not working

Should I add any other config?

回答1:

Check the following areas within Eclipse:

  1. Right Click Project > Properties > Project Facets > Java > Version 1.7

  2. Right Click Project > Properties > Java Build Path > Libraries > JRE Library should be 1.7

  3. Right Click Project > Properties > Java Compiler > Compiler compliance level

  4. Window > Preferences > Server > Runtime Environment > Select the Server > Edit > Ensure JRE is set to 1.7



回答2:

So the only way it seems now is that your application server, eg tomcat is configured for jdk version lower than 1.7. check what version of java is being pointed by JAVA_HOME environment variable on your system.If you correct that, it should solve your problem.



回答3:

I know it's been over 2 years since this thread was last active but in case someone is looking for an answer and the above checks don't solve it: it's because the compiler that your tomcat is running is older than 1.7. One way to solve this is to add this to tomcat/conf/web.xml:

<servlet>
  <servlet-name>jsp</servlet-name>
  <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
  <init-param>
      <param-name>fork</param-name>
      <param-value>false</param-value>
  </init-param>
  <init-param>
      <param-name>xpoweredBy</param-name>
      <param-value>false</param-value>
  </init-param>
  <init-param>                                    <!-- this should be added -->
      <param-name>compilerSourceVM</param-name>
      <param-value>1.7</param-value>
  </init-param>
  <init-param>
      <param-name>compilerTargetVM</param-name>
      <param-value>1.7</param-value>
  </init-param>                                   <!-- last added line -->
  <load-on-startup>3</load-on-startup>
</servlet>

Source