Eclipse JRE System Library [J2SE-1.5] - java

I'm using Eclipse EE 3.7 with m2e plugin installed. I have JDK7 set in eclipse.
When I import maven projects, the JRE is set to JRE System Library [J2SE-1.5], So i have compilation issues with java 6 related stuff. Instead I want the JRE in eclipse to be by default set to JRE System Library [J2SE-1.6]
When i try to open a new project in eclipse File -> new -> Java project on the first screen i have an option to choose JRE and the third option is Use default JRE (currently 'jdk1.7.0_03')
From this i can see that the default JRE in Eclipse is 1.7, but when i import new Maven projects, the JRE is set to 1.5 by default.
Any help, how can i do this?

The problem is not with Eclipse, but with the projects you're importing. m2e will set the project's JRE to match the maven project. The POM specifies the JRE version, and this is defaulted to 1.5 if not present. You need this in the POM:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>

artbristol gave the correct answer (and I upvoted him).
That was in 2012. Here is an update more appropriate for today (2016, Java 8, Spring 4.x/Servlet 3.x):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>

The root cause of this issue is Eclipse cannot resolve a valid value for the maven.compiler.source property when updating the .classpath file from the pom, it is simply using default one i.e
org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5.
Just Add following properties into you pom.xml and update project:
<properties>
<javaVersion>1.8</javaVersion>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>

You can have your brand new Maven project in Eclipse have JRE System Library version other than JavaSE-1.5. For that,find below jar in the following location.
File: maven-compiler-plugin-3.1.jar
Location:
.m2\repository\org\apache\maven\plugins\maven-compiler-plugin\3.1
Then unzip the jar and locate "META-INF\maven\plugin.xml" which has four occurrences of default-value="1.5". Replace all four 1.5 with 1.8 or whatever the version you want. You would know the rest of the process.
Reference

Related

Change Java Version from 1.8 to 1.6 in maven project in intellij [duplicate]

I'm new to both Maven and IntelliJ IDEA.
I have a Maven project written in Java 8. Whenever I try to build it (Maven Projects window -> Lifecycle -> compile -> Run Maven Build) I get a series of compilation errors:
[ERROR] path/to/file.java:[26,52] lambda expressions are not supported in -source 1.5
(use -source 8 or higher to enable lambda expressions)
Where am I supposed to change the value of the -source parameter? I tried adding it as an additional parameter in Settings -> Compiler - > Java Compiler, but I got the same results.
The project's and module's language levels are both set to 8.0.
I'm using Maven 3.2.3 and IntelliJ IDEA Community Edition 13.1.2.
Or easier, add this to your pom's properties section:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
I don't think any response to the question addressed the concern - ". . . in IntelliJ".
Here are the steps: -
Go to Preference(or Settings) in IntelliJ ( or Shortcut on Mac ⌘ + ,)
Build, Execution, Deployment > Build Tools > Maven > Importing - select the "JDK for Importer" dropdown then select your preferred java version, Click Apply
Build, Execution, Deployment > Maven > Runner - select the "JRE" dropdown then select your preferred java version, Click Apply
Click OK
Summary:
'maven-compiler-plugin' ALWAYS work! It is what I suggest you to use.
To change the language level, make usage of
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.4</source>
<target>1.4</target>
</configuration>
</plugin>
</plugins>
</build>
The properties do not always change the language level of Intellij!
In the code below, 1.4 was configured in the pom using maven-compiler-plugin
(the jdk of Intellij is 1.8) and the language level of the project was changed accordingly to 1.4:
It was double-checked! It is an example. Most of the time you will not downgrade the version of the JDK to 1.4!
Of course if you use properties, let's say you put in the pom 1.8, then if you use a 1.8 JDK in Intellij(the language level default is 1.8 or the language default was changed manually), then you will be able to code in 1.8 BUT at the mvn compile, the properties will NOT be seen and you will default to Maven 1.5 and the compilation will NOT succeed !
Change the source as shown below in pom.xml
<build>
<finalName>MQService</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
Adding below lines to root(project level) pom.xml worked me to resolve above issue: (both of the options worked for me)
Option 1:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
Option 2:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
originally posted at: IntelliJ IDEA 13 uses Java 1.5 despite setting to 1.7
It may sometimes happen that after configuring maven in Intellij and changing as following it does not work by command build, so build it by Intellij maven tool.
Setting > Maven > Importer - select the `JDK`
Setting > Maven > Runner - select the `JRE`
After that, try to build by Intellij maven tool instead of Intellij console.
There are two ways of doing this :
First- Add Properties
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
second- Add Plugin
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
Here are the steps for intellij version# 2021.2 Ultimate Edition: -
Go to Settings in IntelliJ
Build, Execution, Deployment > Build Tools > Maven > importing > "JDK for Importer" then select your preferred java version, Click Apply
Build, Execution, Deployment > Build Tools > Maven > Runner > For "JRE" option select your preferred java version,
Click Apply
Click OK
You should add below code in your pom.xml to force the language level to change
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
now intelliJ 2019.3 CE does this for you if you go to import then alt+enter where you will get an option saying "change language level to 8 to use this functionality"
open the ubuntu terminal goto your root directory and type:
export JAVA_HOME = <path to jdk>
for example, it works fine for me {do the same in IntelliJ terminal}.
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
to check the set value type echo $JAVA_HOME
to check the maven version type: mvn -version
you can find all path of JDKs by typing this command, and you can set JDK version.
sudo update-alternatives --config java
also check you have same java -version and javac -version.
You can click on the maven view in the extreme right, then Lifecycle -> right click on install and Modify Run Configuration:
Then expand Java Options, disable Inherit from settings and select your desired Java version.

Specifying Java version in maven - differences between properties and compiler plugin

I'm not very experienced with Maven and while experimenting with multi-module project I started wondering how can I specify Java version for all my child modules in parent Maven pom. Until today I was using just:
<properties>
<java.version>1.8</java.version>
</properties>
...but when researching I found that you can also specify Java version in Maven compiler plugin, like that:
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
And then wrap this into plugin management tag to enable child poms usage of this. So the first question is this:
What are the differences beetwen setting Java version in properties and in Maven compiler plugin?
I couldn't find clear answer but in process of researching I found that you can also specify Java version in this way:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
...which suggest that compiler plugin is there even if I don't explicit declare it. Running mvn package outputs with
maven-compiler-plugin:3.1:compile (default-compile) # testproj ---
...and some other plugins that I didn't declare.
So are those plugins default, hidden part of Maven pom? Are there any differences between setting source/target in properties and in Maven plugin configuration element?
Some other questions are - which way should be used (and when if they are not equal)? Which one is best for multi-module project and what happens if Java version specified in pom is different than version pointed in JAVA_HOME?
How to specify the JDK version?
Use any of three ways: (1) Spring Boot feature, or use Maven compiler plugin with either (2) source & target or (3) with release.
Spring Boot
<java.version> is not referenced in the Maven documentation.
It is a Spring Boot specificity.
It allows to set the source and the target java version with the same version such as this one to specify java 1.8 for both :
1.8
Feel free to use it if you use Spring Boot.
maven-compiler-plugin with source & target
Using maven-compiler-plugin or maven.compiler.source/maven.compiler.target properties are equivalent.
That is indeed :
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
is equivalent to :
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
according to the Maven documentation of the compiler plugin
since the <source> and the <target> elements in the compiler configuration use the properties maven.compiler.source and maven.compiler.target if they are defined.
source
The -source argument for the Java compiler.
NOTE: Since 3.8.0 the default value has changed from 1.5 to 1.6. Since 3.9.0 the default value has changed from 1.6 to 1.7
Default value is: 1.7.
User property is: maven.compiler.source.
target
The -target argument for the Java compiler.
NOTE: Since 3.8.0 the default value has changed from 1.5 to 1.6. Since 3.9.0 the default value has changed from 1.6 to 1.7
Default value is: 1.6.
User property is: maven.compiler.target.
About the default values for source and target, note that
since the 3.8.0 of the maven compiler, the default values have changed from 1.5 to 1.6.
maven-compiler-plugin with release instead of source & target
The maven-compiler-plugin 3.6 and later versions provide a new way :
org.apache.maven.plugins
maven-compiler-plugin
3.8.0
9
You could also declare just :
<properties>
<maven.compiler.release>9</maven.compiler.release>
</properties>
But at this time it will not work as the maven-compiler-plugin default version you use doesn't rely on a recent enough version.
The Maven release argument conveys release : a new JVM standard option that we could pass from Java 9 :
Compiles against the public, supported and documented API for a
specific VM version.
This way provides a standard way to specify the same version for the source, the target and the bootstrap JVM options.
Note that specifying the bootstrap is a good practice for cross compilations and it will not hurt if you don't make cross compilations either.
Which is the best way to specify the JDK version?
The first way (<java.version>) is allowed only if you use Spring Boot.
For Java 8 and below :
About the two other ways : valuing the maven.compiler.source/maven.compiler.target properties or using the maven-compiler-plugin, you can use one or the other. It changes nothing in the facts since finally the two solutions rely on the same properties and the same mechanism : the maven core compiler plugin.
Well, if you don't need to specify other properties or behavior than Java versions in the compiler plugin, using this way makes more sense as this is more concise:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
From Java 9 :
The release argument (third point) is a way to strongly consider if you want to use the same version for the source and the target.
What happens if the version differs between the JDK in JAVA_HOME and which one specified in the pom.xml?
It is not a problem if the JDK referenced by the JAVA_HOME is compatible with the version specified in the pom but to ensure a better cross-compilation compatibility think about adding the bootstrap JVM option with as value the path of the rt.jar of the target version.
An important thing to consider is that the source and the target version in the Maven configuration should not be superior to the JDK version referenced by the JAVA_HOME.
A older version of the JDK cannot compile with a more recent version since it doesn't know its specification.
To get information about the source, target and release supported versions according to the used JDK, please refer to java compilation : source, target and release supported versions.
How handle the case of JDK referenced by the JAVA_HOME is not compatible with the java target and/or source versions specified in the pom?
For example, if your JAVA_HOME refers to a JDK 1.7 and you specify a JDK 1.8 as source and target in the compiler configuration of your pom.xml, it will be a problem because as explained, the JDK 1.7 doesn't know how to compile with.
From its point of view, it is an unknown JDK version since it was released after it.
In this case, you should configure the Maven compiler plugin to specify the JDK in this way :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerVersion>1.8</compilerVersion>
<fork>true</fork>
<executable>D:\jdk1.8\bin\javac</executable>
</configuration>
</plugin>
You could have more details in examples with maven compiler plugin.
It is not asked but cases where that may be more complicated is when you specify source but not target. It may use a different version in target according to the source version. Rules are particular : you can read about them in the Cross-Compilation Options part.
Why the compiler plugin is traced in the output at the execution of the Maven package goal even if you don't specify it in the pom.xml?
To compile your code and more generally to perform all tasks required for a maven goal, Maven needs tools. So, it uses core Maven plugins (you recognize a core Maven plugin by its groupId : org.apache.maven.plugins) to do the required tasks : compiler plugin for compiling classes, test plugin for executing tests, and so for... So, even if you don't declare these plugins, they are bound to the execution of the Maven lifecycle.
At the root dir of your Maven project, you can run the command : mvn help:effective-pom to get the final pom effectively used. You could see among other information, attached plugins by Maven (specified or not in your pom.xml), with the used version, their configuration and the executed goals for each phase of the lifecycle.
In the output of the mvn help:effective-pom command, you could see the declaration of these core plugins in the <build><plugins> element, for example :
...
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>default-clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>default-testResources</id>
<phase>process-test-resources</phase>
<goals>
<goal>testResources</goal>
</goals>
</execution>
<execution>
<id>default-resources</id>
<phase>process-resources</phase>
<goals>
<goal>resources</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<executions>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
...
You can have more information about it in the introduction of the Maven lifeycle in the Maven documentation.
Nevertheless, you can declare these plugins when you want to configure them with other values as default values (for example, you did it when you declared the maven-compiler plugin in your pom.xml to adjust the JDK version to use) or when you want to add some plugin executions not used by default in the Maven lifecycle.
None of the solutions above worked for me straight away. So I followed these steps:
Add in pom.xml:
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
Go to Project Properties > Java Build Path, then remove the JRE
System Library pointing to JRE1.5.
Force updated the project.
The below steps work for me like charm! so thought to share with everyone.
These are the lines i added in the pom.xml file to work with a basic project. I am using Java 12 (you can replace yours 11, 10, 1.8 etc).
<properties>
<maven.compiler.source>12</maven.compiler.source>
<maven.compiler.target>12</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>12</release>
</configuration>
</plugin>
</plugins>
</build>
After changing the pom file please reload your project so that IDE can download/fetch the plugin to the project. (For IntelijIDEA: Right-click on pom.xml -> Go to maven -> Reload project).
please make sure to configure the desire version in your IDE as well.
if you are using IntelliJ idea maven build.
Consider the alternative:
<properties>
<javac.src.version>1.8</javac.src.version>
<javac.target.version>1.8</javac.target.version>
</properties>
It should be the same thing of maven.compiler.source/maven.compiler.target but the above solution works for me, otherwise the second one gets the parent specification (I have a matrioska of .pom)
For NetBeans IDE, changing project properties - (Jersey Service) - Categories > Sources >
Selected 'Source/Binary Format' as 1.8.

how to add jre to to my maven project dependency

I am working on developing a java application which will be scheduled in control-m tool.
the program is configurable without modifying source code,meaning that has the ability to specify log file path, data input file for the program.
control-m or the windows shceduler may or may not have Jre or JDK installed on those machines. What I am thinking to do is to include jre related jars in my project jar(maven module) by specifying it as dependency in my pom.xml so that I don't need to worry whether java installed on those machines from which the program can run.
I am using jdk 1.6.
You can specify this plugin in pom.xml for java dependency.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
<executable>${env.JAVA_HOME}/bin/javac</executable>
<fork>true</fork>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
For this JRE must be installed and JAVA_HOME path must be set on system machine.

Eclipse chokes on enum, even though Compiler compliance level is 1.6

I have set my Eclipse project's Compiler compliance level to 1.6
But I get errors like in the screenshot below: Syntax error, annotations are only available if source level is 1.5.
I have refreshed/cleaned/restarted Eclipse at least 10 times.
It compiles fine with Maven2. The Eclipse project has been generated by Maven2.
The project uses java-6-openjdk
Other solution:
1) Set Workspace JRE to 1.5 (and apply/save). Projects will rebuild.
2) Set Workspace JRE back to 1.6 (and apply/save). Projects will rebuild.
All errors are then gone.
Deleting the Eclipse project and re-importing it solved the problem.
Uncheck Enable project specific settings , click apply. Eclipse will ask for a build , click ok . Errors will be gone after the build .
Also , add the following in your pom.xml
`<properties>
<jdk.version>1.8</jdk.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
</plugins>
</build>
`

setting -source to 1.5, it is set to 1.3 apparently

I am using eclipse, with maven2 plugin.
I am trying to setup a simple annotations based spring 3 mvc web application.
So I went to RunAs and clicked on 'maven build', I set the goal as 'compile'.
When it compiles, I get the error message:
E:\dev\eclipse\springmvc2\src\main\java\web\HomeController.java:[5,1] annotations are not supported in -source 1.3
(use -source 5 or higher to enable annotations)
#Controller
so far I modified the eclipse.ini to use the jdk. I also made sure under preferences, it is at java 1.6.
Not sure where else to change the java version?
(I am assuming source 1.3 means java 1.3 and that I need it to be at least version 1.5 compatible)
You should also set a proper source version in pom.xml (because maven can make builds without Eclipse, so it can't use Eclipse preferences):
<project ...>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
...
</project>

Categories

Resources