Reputation: 2051
I have the following configuration in my system:
Apache Maven 3.5.2 Maven home: /usr/share/maven Java version: 1.8.0_162, vendor: Oracle Corporation Java home: /usr/lib/jvm/java-8-openjdk-amd64/jre Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "4.15.0-20-generic", arch: "amd64", family: "unix"
When I compile (using maven) my project that have Try-with-Resources I get the following error:
/path/driver.java:[29,13] try-with-resources is not supported in -source 1.5
[ERROR] (use -source 7 or higher to enable try-with-resources)
I have tryed to add the flag -source 7 but thats not the way to solve the problem cause it give me this other error:
[ERROR] Error executing Maven.
[ERROR] The specified user settings file does not exist: /path/ource
I have search in internet for the first error and I didn't found anything
Upvotes: 1
Views: 545
Reputation: 21124
If you want to use the Java 8 language features (-source 1.8) and also want the compiled classes to be compatible with JVM 1.8 (-target 1.8), you can either add the two following properties, which are the default property names for the plugin parameters:
<project>
[...]
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
[...]
</project>
or configure the plugin directly:
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Check out this article for further details. However try-with-resources
was introduced in JDK 1.7 (internal) version.
Upvotes: 3
Reputation: 3260
One option already suggested by @Ravindra. Another option is to add properties.
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
See maven-compiler-plugin refeerence.
Upvotes: 1
Reputation: 79
To answer the first part, add the following lines to the POM to set language level
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
After adding those lines you can successfully build your jar, although when you run it your jar will give a no main manifest attribute error.
This can either be fixed by running like java -cp app.jar
com.somepackage.SomeClass
or to correct this and make an executable jar, make your pom look like
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>fully.qualified.main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
Upvotes: 1