Exception in thread "main" java.lang.NoSuchMethodError: java.nio.ByteBuffer.flip()Ljava/nio/ByteBuffer

garykwwong
:

I have a method as of below which has been running properly for a long time:

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();
}

However, today after I have compiled and run the program on a server, below exception was logged when starting the program. It shows a flip() method is not found:

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)

Anybody has any idea on it please?

The program running environment specification is like this:

Server:

  1. openjdk version "1.8.0_242"

Development:

  1. IDE: Version: 2019-09 R (4.13.0)

  2. JDK: jdk-11.0.1

  3. maven: apache-maven-3.3.3 (with below configuration applied)

   <source>1.8</source>   
   <target>1.8</target>
garykwwong
:

After searching for a while and verified through switching the installed JDK between 8 and 11, I have found that there are some changes (new overridden methods) applied to several methods (e.g. flip(), clear() ) in ByteBuffer class.

In Java 8, while calling flip() method of ByteBuffer class, since it has no implementation for this method, so it is actually calling the method from extended class, Buffer; which is returning Buffer object as below:

In Buffer class:

public final Buffer flip() {
    limit = position;
    position = 0;
    mark = -1;
    return this;
}

However, in Java 11, ByteBuffer class has implemented its own flip() method, and the returning object is changed from Buffer to ByteBuffer (This change should be started from Java 9):

In ByteBuffer class:

ByteBuffer flip() {
    super.flip();
    return this;
}

Since I'm using JDK 11 (higher JDK version) to compile the program for running on Java 8, the mentioned exception would occasionally encountered accordingly to javadoc:

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

The statement could be referenced here: http://openjdk.java.net/jeps/247



So, for resolving this kind of problems, the are 2 ways to do so:


Approach 1

One can handle in the duration of compilation, by making use of a newly introduced command-line option:

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>.  

Approach 2

Or we can handle it in codes, as precaution methods, by explicitly casting the ByteByffer as Buffer before calling corresponding methods:

((Buffer) bb).flip();

which in order to force it calling extended class's method (in case the compilation process hasn't taken the new command-line options into consideration):

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();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Exception in thread "main" java.lang.NoSuchMethodError: java.nio.ByteBuffer.flip()Ljava/nio/ByteBuffer

From Dev

Exception in thread "main" java.lang.NoSuchMethodError: com.fasterxml.jackson.core.JsonFactory.requiresPropertyOrdering()Z

From Dev

Exception in thread "main" java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index;

From Dev

Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkState(ZLjava/lang/String;Ljava/lang/Object;)

From Dev

Exception in thread "main" java.lang.StackOverflowError

From Dev

Exception in thread "main" java.lang.IllegalMonitorStateException

From Dev

Exception in thread "main" java.lang.UnsupportedClassVersionError:

From Dev

Exception in thread "main" java.lang.IndexOutOfBoundsException:

From Dev

Exception in thread "main" java.lang.NoClassDefFoundError: Main$1 java?

From Dev

Spark 1.3.1 SQL Lib: Exception in thread "main" java.lang.NoSuchMethodError: org.apache.spark.sql.SQLContext.implicits()

From Java

Kafka with Zookeeper 3.5.7 Crash NoSuchMethodError: java.nio.ByteBuffer.flip()

From Dev

Kafka with Zookeeper 3.5.7 Crash NoSuchMethodError: java.nio.ByteBuffer.flip()

From Dev

PDFbox Exception - Exception in thread "main" java.lang.VerifyError

From Dev

Java: (Error) Exception in thread "main" java.lang.NullPointerException at Main.main(Main.java:14)

From Dev

This in Java - Exception in thread "main" java.lang.StackOverflowError

From Dev

Java - Recursion - Exception in thread "main" java.lang.StackOverflowError

From Dev

java Runnable - Exception in thread "main" java.lang.NoClassDefFoundError

From Dev

java error: Exception in thread "main" java.lang.NoClassDefFoundError

From Dev

"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0" java error

From Dev

Exception in thread "main" java.lang.NumberFormatException: For input string: "" in Java

From Dev

Java "java.lang.NoSuchMethodError: main"

From Dev

Java "java.lang.NoSuchMethodError: main"

From Dev

Exception in thread "main" java.lang.IncompatibleClassChangeError: Implementing class

From Dev

Exception in thread "main" java.lang.NumberFormatException: For input string: "1"

From Dev

Got Exception in thread "main" java.lang.IndexOutOfBoundsException: No group 1

From Dev

"Exception in thread "main" java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing"

From Dev

Exception in thread "main" java.lang.ClassNotFoundException: WordCount

From Dev

"Exception in thread "main" java.lang.NullPointerException" - CSV to Arff

From Dev

Exception in thread "main" java.lang.NoClassDefFoundError: junit/textui/ResultPrinter

Related Related

  1. 1

    Exception in thread "main" java.lang.NoSuchMethodError: java.nio.ByteBuffer.flip()Ljava/nio/ByteBuffer

  2. 2

    Exception in thread "main" java.lang.NoSuchMethodError: com.fasterxml.jackson.core.JsonFactory.requiresPropertyOrdering()Z

  3. 3

    Exception in thread "main" java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index;

  4. 4

    Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkState(ZLjava/lang/String;Ljava/lang/Object;)

  5. 5

    Exception in thread "main" java.lang.StackOverflowError

  6. 6

    Exception in thread "main" java.lang.IllegalMonitorStateException

  7. 7

    Exception in thread "main" java.lang.UnsupportedClassVersionError:

  8. 8

    Exception in thread "main" java.lang.IndexOutOfBoundsException:

  9. 9

    Exception in thread "main" java.lang.NoClassDefFoundError: Main$1 java?

  10. 10

    Spark 1.3.1 SQL Lib: Exception in thread "main" java.lang.NoSuchMethodError: org.apache.spark.sql.SQLContext.implicits()

  11. 11

    Kafka with Zookeeper 3.5.7 Crash NoSuchMethodError: java.nio.ByteBuffer.flip()

  12. 12

    Kafka with Zookeeper 3.5.7 Crash NoSuchMethodError: java.nio.ByteBuffer.flip()

  13. 13

    PDFbox Exception - Exception in thread "main" java.lang.VerifyError

  14. 14

    Java: (Error) Exception in thread "main" java.lang.NullPointerException at Main.main(Main.java:14)

  15. 15

    This in Java - Exception in thread "main" java.lang.StackOverflowError

  16. 16

    Java - Recursion - Exception in thread "main" java.lang.StackOverflowError

  17. 17

    java Runnable - Exception in thread "main" java.lang.NoClassDefFoundError

  18. 18

    java error: Exception in thread "main" java.lang.NoClassDefFoundError

  19. 19

    "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0" java error

  20. 20

    Exception in thread "main" java.lang.NumberFormatException: For input string: "" in Java

  21. 21

    Java "java.lang.NoSuchMethodError: main"

  22. 22

    Java "java.lang.NoSuchMethodError: main"

  23. 23

    Exception in thread "main" java.lang.IncompatibleClassChangeError: Implementing class

  24. 24

    Exception in thread "main" java.lang.NumberFormatException: For input string: "1"

  25. 25

    Got Exception in thread "main" java.lang.IndexOutOfBoundsException: No group 1

  26. 26

    "Exception in thread "main" java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing"

  27. 27

    Exception in thread "main" java.lang.ClassNotFoundException: WordCount

  28. 28

    "Exception in thread "main" java.lang.NullPointerException" - CSV to Arff

  29. 29

    Exception in thread "main" java.lang.NoClassDefFoundError: junit/textui/ResultPrinter

HotTag

Archive