'error "class file has wrong version 55.0, should be 52.0" when building Alfresco

I Generated a project from the Maven Alfresco archetypes:

mvn archetype:generate -Dfilter=org.alfresco:

when I try to build it fails:

[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /D:/Mehrdad/alfresco/alfresco-platform/src/main/java/com/mehr/alfresco/platformsample/HelloWorldWebScript.java:[20,49] cannot access org.springframework.extensions.webscripts.Cache
  bad class file: C:\Users\mehrdad.s\.m2\repository\org\alfresco\surf\spring-webscripts\7.9\spring-webscripts-7.9.jar(org/springframework/extensions/webscripts/Cache.class)
    class file has wrong version 55.0, should be 52.0
    Please remove or make sure it appears in the correct subdirectory of the classpath.
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for AIO - SDK 4.0 1.0-SNAPSHOT:
[INFO]
[INFO] AIO - SDK 4.0 ...................................... SUCCESS [  1.063 s]
[INFO] Alfresco Platform/Repository JAR Module ............ FAILURE [01:54 min]
[INFO] Alfresco Share JAR Module .......................... SKIPPED
[INFO] Integration Tests Module ........................... SKIPPED
[INFO] Alfresco Platform/Repository Docker Module ......... SKIPPED
[INFO] Alfresco Share Docker Module ....................... SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  02:02 min
[INFO] Finished at: 2020-03-10T09:42:54+03:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project alfresco-platform: Compilation failure
[ERROR] /D:/Mehrdad/alfresco/alfresco-platform/src/main/java/com/mehr/alfresco/platformsample/HelloWorldWebScript.java:[20,49] cannot access org.springframework.extensions.webscripts.Cache
[ERROR]   bad class file: C:\Users\mehrdad.s\.m2\repository\org\alfresco\surf\spring-webscripts\7.9\spring-webscripts-7.9.jar(org/springframework/extensions/webscripts/Cache.class)
[ERROR]     class file has wrong version 55.0, should be 52.0
[ERROR]     Please remove or make sure it appears in the correct subdirectory of the classpath.

D:\Mehrdad\alfresco>java -version
java version "1.8.0_231"
Java(TM) SE Runtime Environment (build 1.8.0_231-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.231-b11, mixed mode)

D:\Mehrdad\alfresco>javac -version
javac 1.8.0_231

as you see the java and javac are 8. I have no idea how to solve this problem.

what is the problem?



Solution 1:[1]

from your stacktrace spring-webscripts-7.9.jar is compiled with jdk 11. so you got this error (class file has wrong version 55.0, should be 52.0).

build version

Solution: Change spring-webscripts-7.9 to spring-webscripts.7.2 as your system has java 8 version. No need upgrade Java 8 to Java 11.

add the below dependency in your pom.xml file

<!-- https://mvnrepository.com/artifact/org.alfresco.surf/spring-webscripts -->
<dependency>
    <groupId>org.alfresco.surf</groupId>
    <artifactId>spring-webscripts</artifactId>
    <version>7.2</version>
</dependency>

I hope this helps.

Solution 2:[2]

It complains that some class files were compiled using Java 11 (which is Java 55.0, List of Java class file format major version numbers?) and you try to compile the rest with Java 8.

You should update your local Java version to at least Java 11 and then try to re-compile again

Solution 3:[3]

Well, I know its late to post answer for this, but if it helps anyone.

So the issue is because you have jar file compiled with different versions of java.

Here "Alfresco Platform/Repository JAR Module" is compiled on different version of java than the rest of the module in the project.

If you keep the version similar across the modules then the error will go away.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 rieckpil
Solution 3 Daniel Paul