Unable to compile java project with maven - java

I'm trying to build IntelliJ Project with maven on java 16.0.1, but it can't compile my project, although IntelliJ is able to do it successfuly. Before that, I used maven to compile a java 15 project, but I decided to update everything to 16.0.1 because of complete mess with different versions of java on my machine, like I was able to compile but not to run generated .jar files and etc.
mvn compile output:
[ERROR] Error executing Maven.
[ERROR] java.lang.IllegalStateException: Unable to load cache item
[ERROR] Caused by: Unable to load cache item
[ERROR] Caused by: Could not initialize class com.google.inject.internal.cglib.core.$MethodWrapper
Here is my pom.xml file:
<?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>TaxiDB</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>16.0.1</release>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
</properties>
<dependencies>
...
</dependencies>
</project>
mvn -version output:
Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 16.0.1, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-16-oracle
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.8.0-50-generic", arch: "amd64", family: "unix"
java -verion output:
java version "16.0.1" 2021-04-20
Java(TM) SE Runtime Environment (build 16.0.1+9-24)
Java HotSpot(TM) 64-Bit Server VM (build 16.0.1+9-24, mixed mode, sharing)

The problem is your Maven version is too old. It's not compatible with JDK 16.
Try to manually install maven version 3.8.1:
https://maven.apache.org/install.html
This will solve your problem without downgrading your JDK version.

I had this issue when running mvn compile on Ubuntu 20.10. Running the same maven command on Ubuntu 21.04 (the latest release) seems to be okay.
You may want to check the version of linux you are running. If running an older version, upgrade to Ubuntu 21.04.

Related

Type org.springframework.boot.maven.RepackageMojo not present

I am trying to perform "mvn clean package" in my java project and facing the following error :
Execution default of goal org.springframework.boot:spring-boot-maven-plugin:3.0.2:repackage failed: Unable to load the mojo 'repackage' in the plugin 'org.springframework.boot:spring-boot-maven-plugin:3.0.2' due to an API incompatibility: org.codehaus.plexus.component.repository.exception.ComponentLookupException: org/springframework/boot/maven/RepackageMojo has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 55.0
[ERROR] -----------------------------------------------------
[ERROR] realm = plugin>org.springframework.boot:spring-boot-maven-plugin:3.0.2
[ERROR] strategy = org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy
My maven version is :
Apache Maven 3.8.7 (b89d5959fcde851dcb1c8946a785a163f14e1e29)
Maven home: /opt/maven
Java version: 11.0.17, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en_IN, platform encoding: UTF-8
OS name: "linux", version: "5.15.0-56-generic", arch: "amd64", family: "unix"
Following pom.xml file mojo package :
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<mainClass>javaclass.java</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
I still faced the error when my maven version was : Apache Maven 3.6.3
Although the problem got resolved when I upgraded my maven java version to 17 but I want to compile the java code with java version 11.
Does anyone know the fix?

Setting Java version for maven build

I am trying to set the java version for a maven build when I execute it from a shell script. Fot some reason it is not picking up the intended java version. Any advise welcome.
I am running on a linux centos 7 os.
I have downloaded java 14 from https://jdk.java.net/14/ (Linux / x64), and extracted it to:
/usr/lib/jvm/jdk-14.0.2
pom.xml
<properties>
<java.version>14</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven.compiler.release>${java.version}</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jsk.version>2.2.3</jsk.version>
<start-class>com.nexct.approvalservice.NexctApprovalServiceApplication</start-class>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<fork>true</fork>
</configuration>
</plugin>
deploy.sh
export JAVA_HOME=/usr/lib/jvm/jdk-14.0.2
export PATH=$JAVA_HOME/bin:$PATH
echo $JAVA_HOME
echo | java -version
echo "maven build ..."
mvn clean install -V -DskipTests -Dhttps.protocols=TLSv1,TLSv1.1,TLSv1.2
output:
/usr/lib/jvm/jdk-14.0.2
openjdk version "14.0.2" 2020-07-14
OpenJDK Runtime Environment (build 14.0.2+12-46)
OpenJDK 64-Bit Server VM (build 14.0.2+12-46, mixed mode, sharing)
maven build ...
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /opt/apache-maven-3.6.3
Java version: 1.7.0_161, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.161-2.6.12.0.el7_4.x86_64/jre
Default locale: en_US, platform encoding: ANSI_X3.4-1968
OS name: "linux", version: "3.10.0-957.21.3.el7.x86_64", arch: "amd64", family: "unix"
[INFO] BUILD FAILURE
Compilation failure
[ERROR] javac: invalid flag: -parameters
[ERROR] Usage: javac <options> <source files>
[ERROR] use -help for a list of possible options
I found a solution:
If I set, this it works. Maven compiles the code.
<executable>/usr/lib/jvm/jdk-14.0.2/bin/javac</executable>
However, this is not ideal because the path to the install is hard-coded, so on other servers with a different JAVA_HOME location, it won't work. Is it possible to reference $JAVA_HOME from within the POM file? and do something like this?
<executable><$JAVA_HOME>/bin/javac</executable>
e.g.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<release>${java.version}</release>
<source>${java.version}</source>
<target>${java.version}</target>
<compilerVersion>${java.version}</compilerVersion>
<fork>true</fork>
<executable>/usr/lib/jvm/jdk-14.0.2/bin/javac</executable>
</configuration>
</plugin>

Build failing due to version only via terminal

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>

Maven Java compiler - compilation error using wrong compliance version

When Running install from Eclipse we have no issues as the compiler version is set to 1.8.
When running mvn install in terminal we get the following error.
ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project servicehelper: Compilation failure
[ERROR] try-with-resources is not supported in -source 1.5
[ERROR] (use -source 7 or higher to enable try-with-resources)
[ERROR]
When using mvn install -X we're seeing -target 1.5
However here are my java and javac versions
mvn -version
Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-03T14:39:06-05:00)
Maven home: /usr/local/Cellar/maven/3.5.0/libexec
Java version: 1.8.0_131, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.12.5", arch: "x86_64", family: "mac"
java -version
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
javac -version
javac 1.8.0_131
I seems like from every version i run that we should have 1.8 compliance but maven target continues to try and install with 1.5 compliance.
Add this plugin to your pom.xml file.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<compilerVersion>1.8</compilerVersion>
</configuration>
</plugin>
The way to go is first to define the version of the used plugin which means:
<project>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
You should always pin plugin versions which means all used plugin in your build.
The other option to define the java version (source/target) can be done via:
<project>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Or you can omit the pluginManagement if you have already pinned the plugin version via a corporate pom parent and only give the above properties.
If you have to run Maven with another JDK than you need to compile/test your code you need to go via toolchains
Or you can define the compiler to use via fork option which means to hard code the location of your JDK in your pom file which is not a good idea.
You can try adding the tag java.version on your pom.xml
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

Running executable jar created by maven-assembly: Error: Could not find or load main class

Trying to create a fat jar including all the dependencies using maven-assembly plugin. Tried a bunch of things, but end up with main class not found error. Seems like I've hit a wall, been here for a few hours now.
pom.xml snippet:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>foobar</groupId>
<artifactId>foobar</artifactId>
<version>1.0-SNAPSHOT</version>
<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>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<outputDirectory>/home/jars/foobar/</outputDirectory>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.foobar.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>...</dependencies>
</project>
Maven commands executed:
mvn clean compile assembly:single
mvn package assembly:single
mvn clean compile package assembly:single
Creates jar in /home/jars/foobar/foobar-1.0-SNAPSHOT-jar-with-dependencies.jar as expected
MANIFEST.INF in META-INF
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: user
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_131
Main-Class: com.foobar.Main
Java commands executed:
java -jar foobar-1.0-SNAPSHOT-jar-with-dependencies.jar
java -cp foobar-1.0-SNAPSHOT-jar-with-dependencies.jar com.foobar.Main
java -jar foobar-1.0-SNAPSHOT-jar-with-dependencies.jar -cp com.foobar.Main
All return the same error: Error: Could not find or load main class com.foobar.Main
OS: Fedora 25
java -version
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131-b12)
OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode
javac -version
javac 1.8.0_121
mvn --version
Apache Maven 3.3.9 (NON-CANONICAL_2016-07-01T11:53:38Z_mockbuild; 2016-07-01T17:23:38+05:30)
Maven home: /usr/share/maven
Java version: 1.8.0_131, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.131-1.b12.fc25.x86_64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.10.13-200.fc25.x86_64", arch: "amd64", family: "unix"
SOLVED:
The issue was that the source code wasn't getting compiled. Couldn't find the source code jar in the fat jar. Seems like maven assumes a certain package structure.
Adding the following to <build> solved the issue:
<sourceDirectory>src/com/foobar</sourceDirectory>
The Maven plugin does restructured the content of the JAR for Spring-boot compatible which can cause the commandline "java -jar ..." not to work properly. As user user2354302 discovered, adding src/com/foobar helped.

Categories

Resources