gpt4 book ai didi

java - 线程中的异常 "main"java.lang.NoSuchMethodError : java. nio.ByteBuffer.flip()Ljava/nio/ByteBuffer

转载 作者:行者123 更新时间:2023-12-01 09:54:23 37 4
gpt4 key购买 nike

我有一个方法如下,它已经正常运行了很长时间:

private String loadFromFile(){

RandomAccessFile inFile = null;
FileChannel inChannel = null;
StringBuilder sb = new StringBuilder();
try {

inFile = new RandomAccessFile(this.latestImageFile, "r");
inChannel = inFile.getChannel();

ByteBuffer bb = ByteBuffer.allocate(2046);
while( inChannel.read(bb) != -1){
bb.flip();

while(bb.hasRemaining()){
char c = (char) bb.get(); // read character at current position and set the pointer to current position + 1
sb.append(c);
}

bb.clear();
}

} catch (IOException e) {
e.printStackTrace();
} finally {
if (inChannel != null) try {inChannel.close(); } catch (IOException e){}
if (inFile != null ) try { inFile.close(); } catch (IOException e) {}
}

return sb.toString();
}

但是,今天在服务器上编译并运行程序后,启动程序时记录了以下异常。它显示未找到 flip() 方法:
Exception in thread "main" java.lang.NoSuchMethodError: java.nio.ByteBuffer.flip()Ljava/nio/ByteBuffer;
at com.rt.stream.s.exch.OBWorker.loadFromFile(OBWorker.java:271)
at com.rt.stream.s.exch.OBWorker.initAndLoadOBs(OBWorker.java:184)
at com.rt.stream.s.exch.OBWorker.<init>(OBWorker.java:145)
at com.rt.stream.s.exch.OBWorkerMgr.initFromProperties(OBWorkerMgr.java:217)
at com.rt.stream.s.exch.OBWorkerMgr.init(OBWorkerMgr.java:132)
at com.rt.stream.s.exch.OBWorkerMgr.main(OBWorkerMgr.java:511)

有人对此有任何想法吗?

程序运行环境规范是这样的:

服务器:
  • openjdk 版本“1.8.0_242”

  • 开发:
  • IDE:版本:2019-09 R (4.13.0)
  • JDK:jdk-11.0.1
  • maven:apache-maven-3.3.3(应用以下配置)
  •    <source>1.8</source>   
    <target>1.8</target>

    最佳答案

    搜索了一段时间,通过在8和11之间切换安装的JDK进行验证,发现有一些变化(new overridden methods)应用于 ByteBuffer 类中的几种方法(例如 flip(), clear() )。
    在 Java 8 中,同时调用 flip() ByteBuffer类的方法,由于没有实现该方法,所以实际上是从扩展类Buffer调用该方法;正在返回 Buffer对象如下:
    缓冲区 类(class):

    public final Buffer flip() {
    limit = position;
    position = 0;
    mark = -1;
    return this;
    }
    但是在Java 11中,ByteBuffer类已经实现了自己的flip()方法,并且返回的对象从Buffer变成了ByteBuffer(这个变化应该从Java 9开始):
    字节缓冲区 类(class):
    ByteBuffer flip() {
    super.flip();
    return this;
    }

    由于我使用 JDK 11(更高的 JDK 版本)来编译程序以在 Java 8 上运行,因此偶尔会遇到相应于 javadoc 的异常:

    By default, however, javac compiles against the most-recent version ofthe platform APIs. The compiled program can therefore accidentally useAPIs only available in the current version of the platform. Suchprograms cannot run on older versions of the platform, regardless ofthe values passed to the -source and -target options. This is along-term usability pain point, since users expect that by using theseoptions they'll get class files that can run on the the platformversion specified by -target.


    该声明可以在这里引用: http://openjdk.java.net/jeps/247


    因此,要解决此类问题,有两种方法:

    方法一
    通过使用新引入的命令行选项,可以在编译期间进行处理:
    i.e.
    javac --release N <source files>

    which is equals to:
    for N < 9: -source N -target N -bootclasspath <documented-APIs-from-N>,
    for N >= 9: -source N -target N --system <documented-APIs-from-N>.

    方法二
    或者我们可以在代码中处理它,作为预防方法,通过在调用相应方法之前将 ByteByffer 显式转换为 Buffer:

    ((Buffer) bb).flip();


    为了强制它调用扩展类的方法(以防编译过程没有考虑新的命令行选项):
    private String loadFromFile(){

    RandomAccessFile inFile = null;
    FileChannel inChannel = null;
    StringBuilder sb = new StringBuilder();
    try {

    inFile = new RandomAccessFile(this.latestImageFile, "r");
    inChannel = inFile.getChannel();

    ByteBuffer bb = ByteBuffer.allocate(2046);
    while( inChannel.read(bb) != -1){
    ((Buffer)bb).flip(); // explicitly casting

    while(bb.hasRemaining()){
    char c = (char) bb.get();
    sb.append(c);
    }

    ((Buffer) bb).clear(); // explicitly casting
    }

    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    if (inChannel != null) try {inChannel.close(); } catch (IOException e){}
    if (inFile != null ) try { inFile.close(); } catch (IOException e) {}
    }

    return sb.toString();
    }

    关于java - 线程中的异常 "main"java.lang.NoSuchMethodError : java. nio.ByteBuffer.flip()Ljava/nio/ByteBuffer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61267495/

    37 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com