[Java ] jdk升级 bug java: -source 8 中不支持 instanceof 中的模式匹配 (请使用 -source 16 或更高版本以启用 instanceof 中的模式匹配)

背景: 原项目使用的是 jdk8 ,最近升级了 jdk17 ,之前一直运行没问题,但是今天使用了 instanceof 新语法,导致运行报错 java: -source 8 中不支持 instanceof 中的模式匹配 (请使用 -source 16 或更高版本以启用 instanceof 中的模式匹配)

原因: maven 编译没有使用到 jdk17

修复方法: 修改 pom.xml 添加 maven 编辑插件, 当然编码过程千变万化,这个方法不一定适合所有情况,仅供参考~

<plugin>
  <groupId>org.apache.maven.pluginsgroupId>
  <artifactId>maven-compiler-pluginartifactId>
  <version>3.11.0version>
  <configuration>
    <source>17source>
    <target>17target>
  configuration>
plugin>

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0modelVersion>
  <parent>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-parentartifactId>
    <version>2.7.8version>
    <relativePath/> 
  parent>
  <groupId>com.examplegroupId>
  <artifactId>show_beanartifactId>
  <version>0.0.1-SNAPSHOTversion>
  <name>show_beanname>
  <description>show_beandescription>
  <properties>
    <java.version>17java.version>
  properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-webartifactId>
    dependency>

    <dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-testartifactId>
      <scope>testscope>
    dependency>
    <dependency>
      <groupId>org.projectlombokgroupId>
      <artifactId>lombokartifactId>
    dependency>
  dependencies>

  <build>
    <plugins>


      <plugin>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-maven-pluginartifactId>
        <configuration>
          <image>
            <builder>paketobuildpacks/builder-jammy-base:latestbuilder>
          image>
        configuration>
      plugin>
    plugins>
  build>

project>

修改后的 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0modelVersion>
  <parent>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-parentartifactId>
    <version>2.7.8version>
    <relativePath/> 
  parent>
  <groupId>com.examplegroupId>
  <artifactId>show_beanartifactId>
  <version>0.0.1-SNAPSHOTversion>
  <name>show_beanname>
  <description>show_beandescription>
  <properties>
    <java.version>17java.version>
  properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-webartifactId>
    dependency>

    <dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-testartifactId>
      <scope>testscope>
    dependency>
    <dependency>
      <groupId>org.projectlombokgroupId>
      <artifactId>lombokartifactId>
    dependency>
  dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-compiler-pluginartifactId>
        <version>3.11.0version>
        <configuration>
          <source>17source>
          <target>17target>
        configuration>
      plugin>

      <plugin>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-maven-pluginartifactId>
        <configuration>
          <image>
            <builder>paketobuildpacks/builder-jammy-base:latestbuilder>
          image>
        configuration>
      plugin>
    plugins>
  build>

project>

你可能感兴趣的:(Java探索者之路,java,bug,开发语言)