我正在维护一个基于netty构建的httpServer库,它使用自定义的netty-codec-http。该库应该适用于Java版本> =8。它在Java 12上可以正常工作,但在Java 8上却不能。

我知道问题很可能与java9上ByteBuffer接口的更改有关。但是,我无法使其正常工作。到目前为止我尝试过的


在java8上构建netty-codec-http和httpServer库(编译器源和目标设置为1.8)并在java8上运行
在java12上构建netty-codec-http和httpServer库(编译器source = 12和target = 1.8)并在java8上运行
将发行版本设置为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>


httpServer库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>

最佳答案

这是使用java9 +进行编译并使用java8运行的结果。原因是在java9中,ByteBuffer重写了某些方法,例如flip(),并在java8返回Buffer时返回了ByteBuffer。用jdk8重新编译代码,问题应该解决。

09-30 14:00
查看更多