在maven中使用定制的netty编解码器时出现NoSuchMethodError(编译器目标为1.8)。

问题描述 投票:0回答:1

我正在维护一个基于netty的httpServer库,它使用了一个自定义的 netty-codec-http. 该库应该适用于java版本>=8,在java12上可以正常工作,但在java8上就不行了。谁能帮忙让它在java8上工作。

我明白这个问题很可能与java9上ByteBuffer接口的变化有关。但是,我无法让它工作。到目前为止,我已经尝试了以下方法

  1. 构建 netty-codec-http 和httpServer库,并在java8上运行(编译器源代码和目标设置为1.8)。

  2. 构建 netty-codec-http 和httpServer库在java12上(编译器source=12,target=1.8),并在java8上运行。

  3. 将版本设置为8

使用库时出现异常

java.lang.NoSuchMethodError: java.nio.ByteBuffer.clear()Ljava/nio/ByteBuffer;
    at io.netty.buffer.PooledByteBuf.internalNioBuffer(PooledByteBuf.java:158)
    at io.netty.buffer.PooledByteBuf._internalNioBuffer(PooledByteBuf.java:188)
    at io.netty.buffer.PooledByteBuf.internalNioBuffer(PooledByteBuf.java:201)
    at io.netty.buffer.PooledByteBuf.setBytes(PooledByteBuf.java:253)
    at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1133)
    at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:350)
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:148)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:714)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:650)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:576)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    at java.lang.Thread.run(Thread.java:745)

Maven设置

//version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /usr/local/Cellar/maven/3.6.3_1/libexec
Java version: 13.0.2, vendor: N/A, runtime: /usr/local/Cellar/openjdk/13.0.2+8_2/libexec/openjdk.jdk/Contents/Home
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.15.4", arch: "x86_64", family: "mac"


//command
cd /../netty
mvn clean install -pl codec-http -am 

netty-codec-http pom.xml。

<?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/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>io.netty</groupId>
    <artifactId>netty-parent</artifactId>
    <version>4.1.49.Final</version>
  </parent>

  <artifactId>netty-codec-http</artifactId>
  <packaging>jar</packaging>
  <version>4.1.49.Final.custom.1.0.0-SNAPSHOT</version>

  <name>Netty/Codec/HTTP</name>

  <properties>
    <javaModuleName>io.netty.codec.http</javaModuleName>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>netty-common</artifactId>
      <version>${project.parent.version}</version>
    </dependency>
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>netty-buffer</artifactId>
      <version>${project.parent.version}</version>
    </dependency>
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>netty-transport</artifactId>
      <version>${project.parent.version}</version>
    </dependency>
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>netty-codec</artifactId>
      <version>${project.parent.version}</version>
    </dependency>
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>netty-handler</artifactId>
      <version>${project.parent.version}</version>
    </dependency>
    <dependency>
      <groupId>com.jcraft</groupId>
      <artifactId>jzlib</artifactId>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-core</artifactId>
    </dependency>
  </dependencies>
</project>

http服务器库pom.xml

<project //...>
//...
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-codec-http</artifactId>
            <version>4.1.49.Final.custom.1.0.0-SNAPSHOT</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.49.Final</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-tcnative-boringssl-static</artifactId>
            <version>2.0.30.Final</version>
            <classifier>linux-x86_64</classifier>
        </dependency>
    </dependencies>
</project>
java maven netty
1个回答
1
投票

这是用java9+编译和java8运行的结果。原因是在java9中ByteBuffer覆盖了一些方法,例如flip(),并返回ByteBuffer,而java8是返回Buffer。用jdk8重新编译你的代码,问题应该就解决了。

© www.soinside.com 2019 - 2024. All rights reserved.