11

I'm trying to launch the jar file I've builded for my Spring Boot project. First I did:

mvn clean package spring-boot:repackage

then I've tried to launch the jar file, and I had the following error:

Exception in thread "main" java.lang.UnsupportedClassVersionError: it/sysdata/helios_backend_admin/HeliosAdminBackendApplication has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0 at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.access$100(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:93) at java.lang.ClassLoader.loadClass(Unknown Source) at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:46) at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)

I've checked and java version is 11 everywhere (at least from what I saw). This is the pom:

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>it.sysdata</groupId>
    <artifactId>helios_backend_admin</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>helios-admin-backend</name>
        <packaging>jar</packaging>
    <description>Helios Backend for Dashboard admin</description>

    <properties>
        <java.version>11</java.version>
        <flowable.version>6.4.1</flowable.version>
        <swagger.version>2.9.2</swagger.version>
        <jwt.version>0.9.1</jwt.version>
        <cron4j.version>2.2.5</cron4j.version>  
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
        </dependency>  

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


        <!-- Flowable -->
        <dependency>
            <groupId>org.flowable</groupId>
            <artifactId>flowable-spring-boot-starter-process</artifactId>
            <version>${flowable.version}</version>
        </dependency>
        <dependency>
            <groupId>org.flowable</groupId>
            <artifactId>flowable-http</artifactId>
            <version>${flowable.version}</version>
        </dependency>  
        <!-- Swagger -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <!-- JWT -->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>${jwt.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>



        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
            <version>3.10.1</version>
        </dependency>

        <dependency>
            <groupId>it.sauronsoftware.cron4j</groupId>
            <artifactId>cron4j</artifactId>
            <version>${cron4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
        </dependency>
    </dependencies>
    <build>
        <finalName>${artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                <source>11</source>
                <target>11</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

These are my settings under Project Properties -> Java Compiler -> JDK Compliance:

enter image description here

Then in Window-> Preferences -> Installed JREs:

enter image description here

UPDATE

Are these settings ok? enter image description here

Usr
  • 2,628
  • 10
  • 51
  • 91
  • 3
    The JDK that is being used to run your application is Java 8. Some of the classes have been built using Java 11. Use Java 11 to run the application. The problem is not in the POM files or the JARs your IDE's compiler configs. The problem is *how you are running it*. – Stephen C May 21 '19 at 09:48
  • So I have to set jdk to 11 and not use jre 11 like now? – Usr May 21 '19 at 09:49
  • _Window-> Preferences -> Installed JREs_ set jdk to 11 not to jre 1.8 as StephenC said. – David Pérez Cabrera May 21 '19 at 09:51
  • 1
    1) JDK and JRE are equivalent when you are running code. 2) There is no JRE in Java 11. They stopped shipping JREs in the Java 11 release. (Oracle JDK and Open JDK both) – Stephen C May 21 '19 at 09:51
  • Hi, I've done this, re-run the maven command and then java -jar, but it's giving me the same error. – Usr May 21 '19 at 09:51
  • What has maven got to do with this? This is about running the code. Not building it. Type `java -version`. What does it say? I bet you its says Java 8! – Stephen C May 21 '19 at 09:52
  • 1
    I want to build a jar and then run it, but when doing java -jar it's giving the error. I've updated the question with the suggested edit, but still not working, it is right like that? – Usr May 21 '19 at 09:53
  • @StephenC java -version actually tells it's 8. But I've set JAVA_HOME as `C:\Program Files\Java\jdk-11.0.2` and in Path I have both `C:\Program Files\Java\jdk-11.0.2\bin ` and `C:\Program Files\Java\jdk1.8.0_201\bin` – Usr May 21 '19 at 09:56
  • If you are running the application from the command line, then the "installed JREs" settings in your IDE are not relevant. – Stephen C May 21 '19 at 09:57
  • 1
    I've set it. I've said it in the comment above. – Usr May 21 '19 at 09:57
  • And I bet you have either set it incorrectly, or you didn't set it in the command shell you are using to launch your app. But I can **guarantee** that this is the reason you are getting Java 11 not Java 8. – Stephen C May 21 '19 at 09:59
  • Java 8 is indeed in front... – Usr May 21 '19 at 10:00
  • Then you didn't restart your shell after setting it. Type `echo %PATH%`. Does it say the right thing? – Stephen C May 21 '19 at 10:01
  • No I've restarted it. It keeps saying it's java 8. I've deleted jdk 8 from path but still doing this – Usr May 21 '19 at 10:02
  • Arghh ... you want Java 11 ... so Java 11 must be ahead of Java 8 – Stephen C May 21 '19 at 10:03
  • Which is already ahead? You have said that both are ahead now. – Stephen C May 21 '19 at 10:04
  • 1
    As I've said, it is already ahead. And also, I've deleted Java 8 from path to exclude the problem, restarted the shell, but keeps saying it's java 8 – Usr May 21 '19 at 10:04
  • If the bin directory containing `java` for Java 8 is not on the shell's PATH then running `java -version` from the shell's command prompt cannot possibly tell you that you are running Java 8. Unless you have done something crazy like dropped symlink to `java` in some directory earlier in the path. Or created a shell alias for `java`. Or created a BAT file called `java.BAT`. But this is all basic "how does the Windows shell work" stuff. – Stephen C May 21 '19 at 10:10
  • Can you please provide your run configurations? Which IDE you are using(eclipse or IDEA)? – Krishnom May 21 '19 at 12:31
  • I dealt with something like this, fixed it by deleting the troublesome package from my user/.m2 repo and downgrading the version for the package in `pom.xml` switching the scope to compile. It seems that Eclipse is kinda lame in that it tries to compile to your specified target but if the code needs a higher version it quietly compiles to that and you get this cryptic situation. – Adam D. Mar 17 '23 at 16:13

10 Answers10

9

In my case, there was a wrong JAVA_HOME setup,
and for me worked the following steps:

  1. Go to Environment Variables
  2. Click Edit when JAVA_HOME is selected
  3. Select the directory where your desired jdk is instaled

This might help to select the correct Java version:

  • Java 1.2 uses major version 46
  • Java 1.3 uses major version 47
  • Java 1.4 uses major version 48
  • Java 5 uses major version 49
  • Java 6 uses major version 50
  • Java 7 uses major version 51
  • Java 8 uses major version 52
  • Java 9 uses major version 53
  • Java 10 uses major version 54
  • Java 11 uses major version 55
  • Java 12 uses major version 56
  • Java 13 uses major version 57
  • Java 14 uses major version 58
  • Java 15 uses major version 59
  • Java 16 uses major version 60
  • Java 17 uses major version 61
  • Java 18 uses major version 62
  • Java 19 uses major version 63
Marko
  • 91
  • 1
  • 1
6

I had similar issues running mvn org.springframework.boot:spring-boot-maven-plugin:run on the command line. It turns out the plugin version was upgraded to something that no longer supports the version of java I was running ( java 11 ). Here is the ERROR I saw:

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:3.0.0-M1:run (default-cli) on project PROJECT_NAME: Execution default-cli of goal org.springframework.boot:spring-boot-maven-plugin:3.0.0-M1:run failed: Unable to load the mojo 'run' in the plugin 'org.springframework.boot:spring-boot-maven-plugin:3.0.0-M1' due to an API incompatibility: org.codehaus.plexus.component.repository.exception.ComponentLookupException: org/springframework/boot/maven/RunMojo 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

I was able to fix this by specifying the version in the command line mvn org.springframework.boot:spring-boot-maven-plugin:2.6.3:run ( notice :2.6.3 )

Scot
  • 319
  • 3
  • 9
5

The real problem was, you built classes before by some upgraded version of JDK than what you currently have.

I too faced a similar issue, which I solved in Eclipse by following steps:

Project Properties (Alt + EnterKey) -> Java Build Path -> "Libraries" section -> 
classpath -> select "JRE System Library" -> Edit -> Execution environment from "JRE System Library" window ->
 Select the exact JDK version you currently have -> Apply -> Apply & Close.
Papai from BEKOAIL
  • 1,469
  • 1
  • 11
  • 17
2

Can you try to add the below maven compiler plugin and check.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
        <release>11</release>
    </configuration>
</plugin>

run the maven and build the jar and try to run.

Sambit
  • 7,625
  • 7
  • 34
  • 65
2

After configuring the version of the compiler to 1.8 (your required version), try the older version of spring boot currently it is 3.0.0 by default but you can try 2.7.6 instead.

enter image description here

2

Since you did not specift plugin version in pom.xml file, it will always go for the latest version. Which is 3.0.0 as of 24 Nov 2022. And it requires Java 17 at least.

So, if your project is lower than Java 17, it wont work.

What I suggest is please use the plugin with the same version of your java project. And specify it in pom.xml.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>your projects java version here.</version>
</plugin>
Kıvılcım
  • 391
  • 3
  • 8
1

I fixed by setting JAVA_HOME environment variable to point it to jdk12 directory on my machine.

Naveed I
  • 21
  • 3
1

Just go find the right java JDK and install it, in your case (55.0 ~ JDk 11) This should work, at least for me.

0
    <build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${spring-boot.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
geekyouth
  • 121
  • 8
0

For me the issue is related to the Maven classpath, IDE by default uses maven bundled or wrapper. you can try these steps:

Locate the Maven folder: Identify the folder where you have downloaded and extracted Apache Maven. Let's assume the path to the Maven folder is C:/apache-maven/apache-maven-3.9.3.

Set the Maven home path in your IDE:

For IntelliJ IDEA:

  1. Go to "File" -> "Settings" (or "Preferences" on macOS).
  2. In the Settings/Preferences dialog, navigate to "Build, Execution, Deployment" > "Build Tools" > "Maven".
  3. In the "Maven home directory" field, enter the path C:/apache-maven/apache-maven-3.9.3. 4.Click "Apply" and then "OK" to save the changes.

For Eclipse:

  1. Go to "Window" > "Preferences".
  2. In the Preferences dialog, navigate to "Maven" > "Installations".
  3. Click on "Add" to add a new Maven installation.
  4. In the "Directory" field, browse and select the Maven folder (C:/apache-maven/apache-maven-3.9.3).
  5. Click "Finish" to save the Maven installation.
  6. Select the newly added Maven installation in the list and click "Apply" and then "OK".
  7. Rebuild your Maven project: After setting the Maven home path, rebuild your Maven project to ensure that the updated Maven configuration takes effect.
Kotana Sai
  • 1,207
  • 3
  • 9
  • 20