Docker UndupportedClassversionError通过终端运行

发布于 2025-02-03 18:28:47 字数 2648 浏览 1 评论 0原文

我有一个想在Docker运行的Spring Boot应用程序。一切都设置为Java版本15(Amazon Corretto)。当我从Intellij启动时,该应用程序在本地运行良好。

我运行以下docker命令:

docker build --tag=MyApplication:latest .

尝试使用此命令在Docker中运行它:

docker run -p8887:8888 MyApplication:latest

但是它给了我这个例外:

    Exception in thread "main" java.lang.UnsupportedClassVersionError: com/example/MyApplicationApplication 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(ClassLoader.java:763)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:151)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:348)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:46)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:108)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88)

但是有趣的是,当我通过Docker Desktop运行图像时,它可以很好地工作。其原因如下:

我通过终端在Intellij启动的图像 我手动在Docker桌面上启动的图像

,您可以看到图像在终端运行时使用Java 8。为什么?我将我的Java_home设置为正确的SDK,而Intellij中的所有内容都设置为Java 15。为什么它尝试在Java 8中运行它?我必须在Docker中更改某些东西吗?

我的Dockerfile中有些错误吗?

FROM amazoncorretto:15
COPY target/MyApplication-1.0.jar MyApplication-1.0.jar
ENTRYPOINT ["java","-jar","/MyApplication-1.0.jar"]

I have a Spring Boot application that I want to run in Docker. Everything is set to Java version 15 (Amazon Corretto). The application runs fine locally when I start it with IntelliJ.

I run these Docker commands:

docker build --tag=MyApplication:latest .

and try to run it in Docker with this command:

docker run -p8887:8888 MyApplication:latest

But it gives me this exception:

    Exception in thread "main" java.lang.UnsupportedClassVersionError: com/example/MyApplicationApplication 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(ClassLoader.java:763)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:151)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:348)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:46)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:108)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88)

But here comes the interesting part, when I run the image via Docker Desktop it works perfectly fine. The reason for this is following:

The image I started in IntelliJ via Terminal
The image I started in IntelliJ via Terminal
The image I started in Docker Desktop manually
The image I started in Docker Desktop manually

As you can see the image is using Java 8 when run through terminal. Why? I have set my JAVA_HOME to the correct SDK and everything in IntelliJ is set to Java 15. Why does it try to run it in Java 8? Do I have to change something in Docker?

Is something in my Dockerfile wrong?

FROM amazoncorretto:15
COPY target/MyApplication-1.0.jar MyApplication-1.0.jar
ENTRYPOINT ["java","-jar","/MyApplication-1.0.jar"]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文