Change JDK in Maven Projects (Eclipse) - java

New Maven projects created in Eclipse (on Windows) use the default JRE System Library "J2SE-1.5".
Default System Library in Maven Project
The project was created the following way:
New Project
Maven -> Maven Project
Archtype: maven-archetype-quickstart
I want to use the JDK 1.8.0, which is my default JDK for non Maven projects.
My JAVA_HOME variable is set to
C:\Program Files\Java\jdk1.8.0_152
I tried switching the system library via Right Click on the System Library and set the path to my JDK 1.8.0. But after a Maven update it resets back to the 1.5.
Other Stackoverflow questions just suggest to set the JAVA_HOME, but that did not work for me.
Specify JDK for Maven to use
I looked for some POM.xml commands to set the library, but couldn't find anything. My POM looks pretty standard right now:
<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.test</groupId>
<artifactId>Test123</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Test123</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
What do I need to change to get the JDK 1.8 as my default Library?

Set the JDK in the pom.xml:
<properties>
<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>
in eclipse do a 'Maven->Update Project'

Open your Eclipse, click on
Windows -> Preferences -> Java -> Installed JREs
Verify that the checked JRE refers to the JDK you want. Otherwise, select the checked JRE and click Edit, and change the path to the JDK home.

The following steps did the job for me on Windows:
Set JAVA_HOME to the new JDK (necessary for Maven)
Add the new JDK as runtime environment in Eclipse (Window/Preferences/Installed JRE’s) and set the new JDK as default
Go to Window/Preferences/Installed JRE’s/Execution Environments and select the JavaSE version of the new JDK by clicking it. Select the new JDK as the default for that JavaSE version by filling the checkbox.
Run Maven/Update Project for your project

Related

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>

Using Git with IntelliJ and Maven

If I wanted to use just the normal git via the command line and not the one in IntelliJ, what do I need to include in the version control so when I download it, I can get the Maven libraries without manually installing them?
Edit: There is no pom.xml file when the libraries are added to an IntelliJ project, so I was wondering what I need to include so Maven inside IntelliJ can download the libraries.
what do I need to include in the version control so when I download it, I can get the Maven libraries without manually installing them?
The pom.xml file does this:
Some of the configuration that can be specified in the POM are the project dependencies, the plugins or goals that can be executed, the build profiles, and so on. Other information such as the project version, description, developers, mailing lists and such can also be specified.
Running mvn install will cause Maven to download your dependencies.
Intellij will automatically understand the changes in the pom files and update libraries of course you should have pom.xml file.
If its a maven based project, you definitely need a pom.xml file like 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/xsd/maven-4.0.0.xsd"
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>testing</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>testing</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Once you have a pom.xml file, you can define dependencies like the JUnit dependency defined above with the version you need and maven will automatically take care of downloading the dependency for the project. Once you add any new dependency to the pom.xml, you can run "mvn clean install" from the directory where you have the pom.xml file so that it installs the new dependency.
Hope this helps.

Java compiler level does not match the version of the installed Java project facet", but it does

I'm updating an ancient WebSphere 5 web appliction, porting it to run in Tomcat, replacing IBM RAD 7.0 with Eclipse Kepler, and using Maven for dependencies.
I am getting the "Faceted Project Problem (Java Version Mismatch)" error. The specific text is "Java compiler level does not match the version of the installed Java project facet.".
Here's where I've checked for compiler version:
I right-click on the project and select Properties. I click "Project Facets", and see my Java facet says 1.7.
I check the "Java Compiler" properties page (I'd like the "Enable project specific settings" box to remain unchecked, but it's checked and when I uncheck it, Maven->Refresh Project, and it's checked again.). The compiler version is grayed out, but it is also 1.7.
Window/Preferences/Java/Compiler is also 1.7, and the "Use default compliance settings" box is checked.
Windows/Preferences/Java/Installed JREs has both jdk1.7.0_71 (checked), and jre7 available.
pom.xml also says 1.7:
<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>
...
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
...
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.opennms.core.snmp</groupId>
<artifactId>org.opennms.core.snmp.joesnmp</artifactId>
<version>0.2.6</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Where am I setting a Java compiler level that "does not match the version of the installed Java project facet"?
I had a similar problem before that I resolved by modifying the eclipse.ini file to ensure Eclipse will run the appropriate JVM.
This is what i added to eclipse.ini (you need to use your exact path to javaw.exe) before the -vmargs option:
-vm
C:/Program Files/Java/jdk1.7.0_71/bin/javaw.exe
I had similar case.. changing "Project Faces" brought no results. In my case helped right click on project -> properties -> java compplier and then uncheck option "use compliance from execution environemnt..." and choose your java version (in my case 1.8)

Categories

Resources