Build failing due to version only via terminal - java

I have a simple maven project which builds fine and runs in Intellij. The Java version is set to version 8 under relevant settings in Intellij.
Under the maven POM file, I have set the following to point to the java version.
<modelVersion>4.0.0</modelVersion>
<properties>
<java.version>1.8</java.version>
<javac.src.version>1.8</javac.src.version>
<javac.target.version>1.8</javac.target.version>
</properties>
<groupId>org.example</groupId>
<artifactId>exa</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
.....
My maven version and info as follows:
Maven home: C:\Users\name\apache-maven-3.6.3\bin\..
Java version: 1.8.0_231, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk1.8.0_231\jre
As mentioned, no issues when run via Intellij. But when I run maven commands via the intellij terminal or normal terminal, I get following error.
Example Maven commands.
mvn clean package
or
mvn verify
Error:
COMPILATION ERROR
/C:/Users/name/projectname/src/main/java/pack/Main.java:[17,51] method references are not supported in -source 1.5
I don't even have java sdk 1.5 on my machine. Can I get some help on where I should change this version to 8 so I can make a maven build please? Thanks.

In your project's pom.xml, it seems the source is configured to java 1.5. Make sure the correct source version is configured in pom.xml.
If you are using maven-compiler-plugin, change the version as below.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
or
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

Related

How to set Java compiler compatibility in a Tycho build?

I am using Tycho to build an RCP application. The build runs in a Gitlab CI script on a Windows Runner. The app is setup to run at Java 8 level due to some components in one of the plugins. The runner uses OpenJDK 12. At the moment, this causes compiler errors.
How can I set the source and target compiler level for the build?
I have tried setting the following in the parent pom:
<properties>
<tycho.version>1.5.1</tycho.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
and also
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-compiler-plugin</artifactId>
<version>${tycho.version}</version>
<configuration>
<compilerVersion>1.8</compilerVersion>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
but I still get compiler errors such as JAXBElement cannot be resolved to a type.
You'll have to set execution environment on the overview tab of the MANIFEST.MF file. Tycho will infer maven properties from there.
...
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
...

Configure Maven to allow a range of JREs

I am teaching a 2nd semester Java course, and I am trying to set up a simple Maven project that will will run in any student's environment (Windows/Mac/Linux, IntelliJ/VS Code/Eclipse, any version of Java 8 or above) without complaining. Because this is only a second semester programming course, I want pom.xml to be as simple as possible. The project need only be a couple of Java source files and a couple of test files.
The current pom.xml is below. It works, but Visual Studio Code on MacOS with Java 8 generates the following warnings:
Build path specifies execution environment J2SE-1.5. There are no
JREs installed in the workspace that are strictly compatible with
this environment.
The compiler compliance specified is 1.5 but a JRE
1.8 is used
I can fix the problem by adding these lines:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
But, won't that cause warnings or errors for students with different Java versions? Since we aren't doing anything fancy, I don't want to require that they all use Java 12 or 13. Any version 8 or above is fine.
I also hesitate to tell the students to edit the properties above to match their machines because I assume that will cause problems if I pull their code and try to run it on my machine.
Is there a way to configure Maven and the IDE to simply be happy with any JDK version 8 and above?
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>gvsucis</groupId>
<artifactId>ArrayLab</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>ArrayLab</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
You need to distinguish between the JDK and the Java version you build for.
Setting maven.compiler.source and maven.compiler.target only sets the target Java version. If you set it to 1.8, then you can build with any JDK that supports this, which are JDK 8 and above.
Please change your java home to use jdk not jre.
if you are using a jdk8+, then your pom configuration should work.
Typically all IDE refer the JAVA_HOME environment variable to find the java binary. So, Set your JAVA_HOME to jdk8+.
if it is widows machine restart is required otherwise just run source .bashrc or source .bach_profile

Cannot change Maven version in Eclipse IDE

After failing to mvn package inside of Eclipse but succeeding in the terminal, I realized that Eclipse was running Maven 3.3.3 while terminal was running 3.3.9. I attempted, and by all apparent accounts succeeded, to reconfigure eclipse by going to Eclipse -> Preferences -> Maven -> Installations and pointing to my 3.3.9 folder. I restarted Eclipse and tried again but to no available. Also, changes to the pom.xml file are not acknowledged when attempting to build. I've inserted inappropriate slashes and other characters and am only able to generate this error:
Error assembling WAR: web.xml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)
I modified my pom file as per this recommendation but as I said, maven is not recognizing changes to the pom and I cannot get running mvn --version to produce 3.3.9 as it should. I've attached my pom.xml below
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.javawebtutor</groupId>
<artifactId>LoginWebApp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>LoginWebApp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
<build>
<finalName>LoginWebApp</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webXml>src\main\webapp\WEB-INF\web.xml</webXml>
</configuration>
</plugin>
</plugins>
</build>
Try setting up a runtime configuration, or see if you're using one already.
In eclipse, right click on the project->run as->run configurations. Look for the m2 Maven Build section on the left. If there is one under your project's name, then edit it, otherwise, create one. On the bottom of the dialog there is a drop down that allows you to select which maven configuration you want to run:
This gives me the following output:
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11 10T11:41:47-05:00)
Maven home: C:\Program Files\Java\apache-maven-3.3.9
Java version: 1.7.0_80, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.7.0_80\jre
Default locale: en_US, platform encoding: UTF-8
OS name: "windows 8.1", version: "6.3", arch: "amd64", family: "windows"
Call cmd, type systempropertiesadvanced Check environment variables:
%MAVEN_HOME%, %PATH%.
Check these steps:
Look at the below screenshot (this is relate to your problem):
EMBEDDED: 3.3.3
outsite Eclipse: 3.3.9
(source: http://codingvn.com/2016/02/08/cau-hinh-de-eclipse-su-dung-ban-cai-dat-maven-ben-ngoai-ide/ this is my website)
Normally you have to reinstall the m2eclipse plugin to run with the correct version. If it was updated, it won't work with the previous one, as it will take the "old" maven version.

Setting JAVA_HOME for Maven on Windows 10

I recently upgraded to Windows 10. I'm trying to create a new maven project in eclipse, but it doesn't seem to be picking up my JAVA_HOME setting.
Here is the JDK installation directory:
And here is my JAVA_HOME environment variable setting:
If I run mvn -version in a command prompt, maven does seem to get the correct value for JAVA_HOME:
I then try to create a new maven project in eclipse, using a barebones pom.xml:
<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>StaticVoidGames</groupId>
<artifactId>DatabaseTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>DatabaseTest</name>
</project>
But the project's JRE is 1.5 for some reason:
For what it's worth, my eclipse workspace default JRE is the same as my JAVA_HOME setting.
I've tried manually changing the build path to use the correct jdk, but it simply reverts. I've tried cleaning the project, doing a maven update, restarting eclipse, restarting my computer, and creating a new project. Maven still chooses Java 1.5, which isn't even installed on my machine.
All of my googling says that maven should pick up the JAVA_HOME setting to figure out which Java to use, but that doesn't seem to be the case here. Is there another setting that I'm missing somewhere?
As per screenshoot, Maven is correctly picking up your JAVA_HOME, however what you see in Eclipse is not the version of Java installed (1.5) but the version Maven will use by default to compile.
In your case, Maven will use JDK 1.8 and compile settings source and target to 1.5 (version by default).
To set Maven to the same level as the Java version installed, you need to configure the Maven Compiler Plugin. Once done that and after an Eclipse update (right click on the project, Maven, Update Project), Eclipse will update its view accordingly.
You can add to your POM the following Maven Compiler Plugin configuration:
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>
Alternatively, you can use well-known Maven properties to achieve the same by only settings:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

IntelliJ target bytecode reverting

For a project that I'm currently working on, IntelliJ gave me the compile error Error:java: javacTask: source release 8 requires target release 1.8. I went into Settings>Build, Execution, Deployment>Compiler>Java and saw that the target bytecode version for one of my modules was set to 1.5, so I changed it to 1.8, compiled, and it worked. But then the next day, I got the same error. I went into settings and the target bytecode for that one module was back at 1.5. I changed it to 1.8 and it compiled/ran just fine. This has now happened multiple times and I am frustrated by the number of times I have to go into settings to manually change the target bytecode version.
Why does the target bytecode version keep reverting? I don't have 1.5 specified in the pom or anywhere else, so I am baffled as to why the bytecode version keeps getting set to 1.5.
You need to specify the source and target versions in your pom.xml file, but since the maven-compiler-plugin is added by default, an easier way to do that would be to set the following properties:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
You need to dd this to your POM:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
FYI.
I still had this problem despite setting the pom.xml with both the settings above from the answers as mentioned by #isapir and #carlos-a-ibarra respectively.
I discovered this was due to an Intellij setting:
This config can be found in Build,Execution,Deployment > Build Tools > Maven > Importing
By default, it is set to the 'Internal JRE' setting, which in the latest version of Idea is 11. I had to select it to use the 1.8 JDK the project was configured to use.
This is annoying, because if you have a mix of 11 and 8 projects, you'll have to manually toggle this setting back and forth.
It caused the module output to always fall back to 11 every time the pom.xml was reimported.
It's a really annoying bug in IntelliJ.
Please try below option, it worked for me.
step 01: Change the version under Module and project.
step 02: build the module.
Hope it should resolve the issue.
I have the same issue with "IntelliJ IDEA 2022.2.3 (Community Edition)".
This is my "pom.xml"
<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
<artifactId>java-test-17</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.release>17</maven.compiler.release>
</properties>
</project>
I can manually change the "Target bytecode version" to 17. (Preference => Build, Execution, Deployment) => Compiler => Java Compiler).
However, if I reload maven project (Right click "pom.xml" => Maven => Reload project), the "Target bytecode version" will be changed back to 1.5.
To prevent that, I added "maven-compiler-plugin" to pom.xml. Then I reload maven project (Right click "pom.xml" => Maven => Reload project). Now, the "Target bytecode version" is 17.
<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
<artifactId>java-test-17</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.release>17</maven.compiler.release>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
</plugin>
</plugins>
</build>
</project>

Categories

Resources