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
Related
I'm trying to run a JavaFX application in IntelliJ. Environment - Ubuntu 18.04. Java - 11.
But it gives me error saying :
Error occurred during initialization of boot layer
java.lang.module.FindException: Module javafx.controls not found
Whole command in the run-console :
/usr/lib/jvm/java-1.11.0-openjdk-amd64/bin/java --module-path
/home/mua/Software/javafx_sdk-1_3_1-b05a-linux-i586/javafx-sdk1.3/lib
--add-modules=javafx.controls,javafx.fxml -Djava.library.path=/home/mua/Software/javafx_sdk-1_3_1-b05a-linux-i586/javafx-sdk1.3/lib -javaagent:/home/mua/Software/ideaIU-2020.3.1/idea-IU-203.6682.168/lib/idea_rt.jar=41593:/home/mua/Software/ideaIU-2020.3.1/idea-IU-203.6682.168/bin
-Dfile.encoding=UTF-8 -classpath /home/mua/Desktop/PhotoSlide/PhotoSlide/target/classes:/home/mua/.m2/repository/org/openjfx/javafx-base/16-ea+7/javafx-base-16-ea+7.jar:/home/mua/.m2/repository/org/openjfx/javafx-base/16-ea+7/javafx-base-16-ea+7-linux.jar:/home/mua/.m2/repository/org/openjfx/javafx-fxml/16-ea+7/javafx-fxml-16-ea+7.jar:/home/mua/.m2/repository/org/openjfx/javafx-fxml/16-ea+7/javafx-fxml-16-ea+7-linux.jar:/home/mua/.m2/repository/org/openjfx/javafx-graphics/16-ea+7/javafx-graphics-16-ea+7.jar:/home/mua/.m2/repository/org/openjfx/javafx-graphics/16-ea+7/javafx-graphics-16-ea+7-linux.jar:/home/mua/.m2/repository/org/openjfx/javafx-media/16-ea+7/javafx-media-16-ea+7.jar:/home/mua/.m2/repository/org/openjfx/javafx-media/16-ea+7/javafx-media-16-ea+7-linux.jar:/home/mua/.m2/repository/org/openjfx/javafx-controls/16-ea+7/javafx-controls-16-ea+7.jar:/home/mua/.m2/repository/org/openjfx/javafx-controls/16-ea+7/javafx-controls-16-ea+7-linux.jar:/home/mua/.m2/repository/org/openjfx/javafx-swing/16-ea+7/javafx-swing-16-ea+7.jar:/home/mua/.m2/repository/org/openjfx/javafx-swing/16-ea+7/javafx-swing-16-ea+7-linux.jar:/home/mua/.m2/repository/org/controlsfx/controlsfx/11.0.2/controlsfx-11.0.2.jar:/home/mua/.m2/repository/org/kordamp/ikonli/ikonli-core/12.1.0/ikonli-core-12.1.0.jar:/home/mua/.m2/repository/org/kordamp/ikonli/ikonli-javafx/12.1.0/ikonli-javafx-12.1.0.jar:/home/mua/.m2/repository/org/kordamp/ikonli/ikonli-themify-pack/12.1.0/ikonli-themify-pack-12.1.0.jar:/home/mua/.m2/repository/org/kordamp/ikonli/ikonli-fontawesome-pack/12.1.0/ikonli-fontawesome-pack-12.1.0.jar:/home/mua/.m2/repository/com/github/dragon66/icafe/1.1-SNAPSHOT/icafe-1.1-20190902.150913-102.jar:/home/mua/.m2/repository/com/github/jai-imageio/jai-imageio-core/1.4.0/jai-imageio-core-1.4.0.jar:/home/mua/.m2/repository/com/twelvemonkeys/imageio/imageio-core/3.6/imageio-core-3.6.jar:/home/mua/.m2/repository/com/twelvemonkeys/common/common-lang/3.6/common-lang-3.6.jar:/home/mua/.m2/repository/com/twelvemonkeys/common/common-io/3.6/common-io-3.6.jar:/home/mua/.m2/repository/com/twelvemonkeys/common/common-image/3.6/common-image-3.6.jar:/home/mua/.m2/repository/com/twelvemonkeys/imageio/imageio-tiff/3.6/imageio-tiff-3.6.jar:/home/mua/.m2/repository/com/twelvemonkeys/imageio/imageio-psd/3.6/imageio-psd-3.6.jar:/home/mua/.m2/repository/com/twelvemonkeys/imageio/imageio-metadata/3.6/imageio-metadata-3.6.jar:/home/mua/.m2/repository/org/slf4j/slf4j-simple/2.0.0-alpha1/slf4j-simple-2.0.0-alpha1.jar:/home/mua/.m2/repository/org/slf4j/slf4j-api/2.0.0-alpha1/slf4j-api-2.0.0-alpha1.jar:/home/mua/.m2/repository/com/h2database/h2/1.4.200/h2-1.4.200.jar:/home/mua/.m2/repository/org/ejml/ejml-simple/0.40/ejml-simple-0.40.jar:/home/mua/.m2/repository/org/ejml/ejml-core/0.40/ejml-core-0.40.jar:/home/mua/.m2/repository/javax/media/jai/com.springsource.javax.media.jai.core/1.1.3/com.springsource.javax.media.jai.core-1.1.3.jar:/home/mua/.m2/repository/org/projectlombok/lombok/1.18.16/lombok-1.18.16.jar:/home/mua/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.12.0/jackson-databind-2.12.0.jar:/home/mua/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.12.0/jackson-annotations-2.12.0.jar:/home/mua/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.12.0/jackson-core-2.12.0.jar
org.photoslide.App
And I've added the VM with :
--module-path /home/mua/Software/javafx_sdk-1_3_1-b05a-linux-i586/javafx-sdk1.3/lib --add-modules=javafx.controls,javafx.fxml
And added libraries too :
Source code : https://github.com/maifeeulasad/PhotoSlide
Java configurations :
>>java --version
openjdk 11.0.10 2021-01-19
OpenJDK Runtime Environment (build 11.0.10+9-Ubuntu-0ubuntu1.18.04)
OpenJDK 64-Bit Server VM (build 11.0.10+9-Ubuntu-0ubuntu1.18.04, mixed mode, sharing)
>>javac --version
javac 11.0.10
From what I understand, you're using Non-modular with Maven.
I built one from scratch myself following the instructions provided in the link above and noticed the picture marked in yellow.
There are 2 Environment Virables that need to be set for the OpenJDK. One in your system (e.g. ./bashrc file) and one in IntelliJ for Maven (in case you're using Java 11 or above, which you are in this case).
As per your post, I know you have the one in your system, but it doesn't mention the one in this picture, so make sure you do.
Also, notice I haven't set up any VM options!
The link above states:
The JavaFX classes will be recognized. Notice also that Maven manages
the required dependencies: it will add javafx.base and
javafx.graphics that are required by javafx.controls and
javafx.fxml, but most important, it will add the required classifier
based on your platform, downloading the specific platform jars.
This is my pom.xml file (auto-generated by IntelliJ when making the project):
<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.company</groupId>
<artifactId>testing_maven</artifactId>
<version>1.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>11.0.2</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>11.0.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.4</version>
<configuration>
<mainClass>com.company.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
I strongly recommend looking through the link I posted. You might've missed something. If not, then definitely try and create a new testing project, follow the instructions and see if it works. If it does, compare things to your current project.
Sorry, I couldn't have been of more help. I don't really work with Maven.
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>
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
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>
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>