Java spring-boot 编译使用 jdk 1.7 但运行使用 1.6

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28983247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:08:16  来源:igfitidea点击:

spring-boot compile use jdk 1.7 but run use 1.6

javaspring-bootjdk1.6

提问by sahmada

I compile spring boot application using jdk 1.7 to application.jar file, but when I run application.jar on machine that installed jdk 1.6, this error is occured

我使用 jdk 1.7 将 spring boot 应用程序编译为 application.jar 文件,但是当我在安装了 jdk 1.6 的机器上运行 application.jar 时,出现此错误

Exception in thread "main" java.lang.UnsupportedClassVersionError: com/saifiahmada/spring/Application : Unsupported major.minor version 51.0

线程“main”中的异常 java.lang.UnsupportedClassVersionError: com/saifiahmada/spring/Application : Unsupported major.minor version 51.0

this is my pom.xml

这是我的 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>com.saifiahmada.spring</groupId>
<artifactId>dashboard</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dashboard tsd</name>
<description>dashboard tsd</description>
<packaging>jar</packaging>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.1.RELEASE</version>
</parent>

<properties>
    <webjars-bootstrap.version>3.2.0</webjars-bootstrap.version>
    <webjars-jquery-ui.version>1.9.2</webjars-jquery-ui.version>
    <webjars-jquery.version>2.1.1</webjars-jquery.version>
    <java.version>1.6</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <maven.compiler.source>${java.version}</maven.compiler.source>
    <maven.compiler.target>${java.version}</maven.compiler.target>
    <tomcat.version>7.0.59</tomcat.version>
</properties>

<dependencies>

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.9</version>
    </dependency>

    <dependency>
        <groupId>org.scala-lang</groupId>
        <artifactId>scala-library</artifactId>
        <version>2.10.4</version>
    </dependency>
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3.1</version>
    </dependency>

</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <!-- <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration> -->
            <dependencies>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>springloaded</artifactId>
                    <version>1.2.0.RELEASE</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

thanks

谢谢

[edit] to @Saurabh, I change dependency in my pom.xml for spring-boot-starter-data-jpa, like this :

[编辑] 到@Saurabh,我在我的 pom.xml 中更改了 spring-boot-starter-data-jpa 的依赖项,如下所示:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>javax.transaction</groupId>
                    <artifactId>javax.transaction-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
<dependency>
            <groupId>org.jboss.spec.javax.transaction</groupId>
            <artifactId>jboss-transaction-api_1.1_spec</artifactId>
            <version>1.0.0.Final</version>
</dependency>

回答by Jesper

Newer Java versions are compatible with older Java versions, but not the other way around. So, you cannot normally run code compiled with JDK 7 on a Java 6 runtime environment.

较新的 Java 版本与较旧的 Java 版本兼容,但反之则不然。因此,您通常无法在 Java 6 运行时环境中运行使用 JDK 7 编译的代码。

The best way to make sure it works on Java 6 is to use JDK 6 instead of JDK 7 to compile your code.

确保它适用于 Java 6 的最佳方法是使用 JDK 6 而不是 JDK 7 来编译您的代码。

That means you will have to download and install JDK 6, and make sure that it is being used when you compile your code instead of JDK 7. (Note: JDK 6 is not supported anymore by Oracle, you can still download it but Oracle requires that you create an account in order to download it).

这意味着您必须下载并安装 JDK 6,并确保在编译代码时使用它而不是 JDK 7。(注意:Oracle 不再支持 JDK 6,您仍然可以下载它,但 Oracle 需要您创建一个帐户以下载它)。

回答by ci_

The issue you're having is that one of your dependencies requires some jar that is compiled against Java 7 (javax.transaction-api-1.2.jar)

您遇到的问题是您的依赖项之一需要一些针对 Java 7 编译的 jar (javax.transaction-api-1.2.jar)

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

Please have a look at this, which describes how to run Spring Boot on Java 6:

请看看这个,它描述了如何在 Java 6 上运行 Spring Boot:

http://docs.spring.io/spring-boot/docs/1.2.2.RELEASE/reference/htmlsingle/#howto-use-java-6

http://docs.spring.io/spring-boot/docs/1.2.2.RELEASE/reference/htmlsingle/#howto-use-java-6

The important bit quoted here for reference:

这里引用的重要一点供参考:

"While the Java Transaction API itself doesn't require Java 7 the official API jar contains classes that have been built to require Java 7. If you are using JTA then you will need to replace the official JTA 1.2 API jar with one that has been built to work on Java 6. To do so, exclude any transitive dependencies on javax.transaction:javax.transaction-api and replace them with a dependency on org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:1.0.0.Final"

“虽然 Java 事务 API 本身不需要 Java 7,但官方 API jar 包含已构建为需要 Java 7 的类。如果您使用的是 JTA,那么您将需要将官方 JTA 1.2 API jar 替换为已安装的 JTA 1.2 API jar。构建用于 Java 6。为此,排除对 javax.transaction:javax.transaction-api 的任何传递依赖项,并将它们替换为对 org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec 的依赖项: 1.0.0.最终版”

Also have a look here:

也看看这里:

http://docs.spring.io/spring-boot/docs/1.2.2.RELEASE/reference/htmlsingle/#getting-started-system-requirements

http://docs.spring.io/spring-boot/docs/1.2.2.RELEASE/reference/htmlsingle/#getting-started-system-requirements

I do realise that you are using Spring Boot 1.2.1 and all my references are for 1.2.2, but it seems likely that this issue existed back in 1.2.1 as well and 1.2.1 is already not available for download anymore, so you may want to consider upgrading. But you will run into trouble with Java 6, so you may as well think about upgrading your environment.

我确实意识到您正在使用 Spring Boot 1.2.1 并且我所有的参考都是针对 1.2.2,但这个问题似乎也存在于 1.2.1 中,并且 1.2.1 已经不再可供下载,所以您可能要考虑升级。但是你会遇到 Java 6 的麻烦,所以你不妨考虑升级你的环境。

FWIW, you don't even need these lines in your pom.xml:

FWIW,您甚至不需要 pom.xml 中的这些行:

<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>