0

This is my simple code from a Maven project which has try-with-resources.I use eclipse as my IDE.

public class Hello {

    public static void main(String[] args) {

        try(FileInputStream fi = new FileInputStream("ANC")){

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

When I build this using clean package -U it gives me the following error.

Hello.java:[11,20] try-with-resources is not supported in -source 1.5
  (use -source 7 or higher to enable try-with-resources). 

However,I have my java compiler to be set at Java 1.8 and in Jave Build path JRE System Library is also JDK 1.8.Still this error persists.

This is my POM file ( minus the junit dependency )

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.commonutil</groupId>
  <artifactId>PropertyFile</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>PropertyFile</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
</project>

Note : Setting the target and source in pom.xml did not help either. Any help regarding this is appreciated.Thank you.

Ran_Macavity
  • 154
  • 2
  • 21
  • @Oleg Tried setting the source and target in pom.xml too.Did not change the outcome. – Ran_Macavity Dec 01 '17 at 11:11
  • Be careful with that your IDE is not synchronized with the pom.xml, so any IDE specific modifications will not apply to maven, if you use maven to build your project. – Ioannis Sermetziadis Dec 01 '17 at 11:15
  • Then you did something wrong, this is the solution. – Oleg Dec 01 '17 at 11:17
  • @YannisSermetziadis Thankl you for the reply.Any idea on how to synchronize the IDE with Maven.I am using maven plugin for eclipse. – Ran_Macavity Dec 01 '17 at 11:18
  • @Ran_Macavity, you can try the maven-eclipse-plugin to generate the eclipse project files based on your pom.xml and maven configuration (settings.xml). From IDE to maven synchronization is probably a job of the IDE to provide, so I do not think there is a standard way. – Ioannis Sermetziadis Dec 01 '17 at 11:28
  • Just a hint: maven-eclipse-plugin is deprecated. Better use a most recent version of m2e integration. That should work.. – khmarbaise Dec 01 '17 at 11:32

1 Answers1

7

You need to specify the java source version, so the correct flag value is used by the compiler plug-in. For example:

<properties>
  <maven.compiler.source>1.8</maven.compiler.source>
</properties>
  • 3
    Not to forget. `1.8`.. – khmarbaise Dec 01 '17 at 11:33
  • In addition to the above information, those two maven-compiler-plugin properties map to the javac -source and -target flags. javac -help -source Provide source compatibility with specified release -target Generate class files for specific VM version – Ioannis Sermetziadis Dec 01 '17 at 13:56