高版本的jdk在使用maven时,如何编译成低版本的class

idea项目sdk设置的jdk17

java 复制代码
private byte[] getUtf8Bytes(char c){
    char[] chars ={c};
    CharBuffer charBuffer = CharBuffer.allocate(chars.length);
    charBuffer.put(chars);
    charBuffer.flip();
    ByteBuffer byteBuffer=StandardCharsets.UTF_8.encode(charBuffer);
    return byteBuffer.arrayO);
}

在pom.xml中指定了maven.compile.source和target

xml 复制代码
 <properties>
        <revision>1.0.0-SNAPSHOT</revision>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spotless.skip>true</spotless.skip>
</properties>

通过mvn打包上传,运行报flip()方法不存在,返回值不一致。 解决方案:

java 复制代码
private byte[] getUtf8Bytes(char c){
    return Character.toString(c).getBytes(StandardCharsets.UTF_8);
}

但是,这引起了我的兴趣,明明我已经指定了source和target为8,为什么没生效

原因: 在 JDK 9 及以上版本javac 的行为变了。

  • 当使用 JDK 9+(比如你现在用 JDK 11)编译时,
    -source-target 已不足以限制生成的 class 版本。
  • 你还必须显式指定 --release 参数,否则 javac 会使用当前 JDK 的标准库(即 JDK 11 的 API),
    导致编译出的字节码即使版本号是 52(Java 8),但依赖了 JDK 11 的类或方法 → 实际上仍然不兼容 JDK 8。

解决方案:

xml 复制代码
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.10.1</version> <!-- 建议显式声明 -->
    <configuration>
        <encoding>UTF-8</encoding>
        <release>8</release>
        <source>8</source>
        <target>8</target>
    </configuration>
</plugin>

或者

xml 复制代码
<maven.compiler.release>8</maven.compiler.release>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>

加入source和target是为了保证在java8(没有release参数)也没问题


先说为什么一个java8的项目,开发者却设置了jdk17而且一直没出问题: 1.项目的parent引入了spotless插件,且仅支持jdk11以上的版本 2.parent项目升级了一个版本之后,引入的redisson要求jdk必须为8 这才暴露了问题。 最开始是改为project sdk = jdk8 maven runner = jdk11 但是编译之后的包还是有问题,临时禁用了spotless <spotless.skip>true</spotless.skip> 但是没有完全生效 于是加入了另一个配置

xml 复制代码
<plugin>
  <groupId>com.diffplug.spotless</groupId>
  <artifactId>spotless-maven-plugin</artifactId>
  <executions>
    <execution>
      <id>default</id>
      <phase>none</phase>  <!-- 禁用所有执行 -->
    </execution>
  </executions>
</plugin>

这样再将project sdk 设置为jdk8 maven runner 设置为project sdk

如果要启用spotless ,maven指定release是最稳妥的办法。

相关推荐
GetcharZp11 小时前
GitHub 2.4 万 Star!D2 正在重新定义程序员画图方式
后端
zhangxingchao13 小时前
多 Agent 架构到底怎么选?从 Claude Agent Teams、Cognition/Devin 到工程落地原则
前端·人工智能·后端
IT_陈寒13 小时前
SpringBoot那个自动配置的坑,害我排查到凌晨三点
前端·人工智能·后端
ServBay13 小时前
OpenCode 和它的7款必备插件
后端·github·ai编程
ping某13 小时前
逐字节拆解 tcpdump
后端
阿凡98073013 小时前
花 100 dollar,用 Claude 打通 EasyEDA&Fusion 双向同步
后端·程序员
irving同学4623813 小时前
从零搭建生产级 RAG:Embedding、Chunking、Hybrid Search 与 Reranker
前端·后端
她的男孩13 小时前
从零搭一个企业后台,为什么我把能力拆成 Starter 和 Plugin
java·后端·架构
胡志辉13 小时前
本地 AI 编码助手从 0 配起来:先选模型,再接 Ollama、VS Code、Claude Code 和 Codex
前端·后端
RainCity13 小时前
Java Swing 自定义组件库分享(七)
java·笔记·后端