I get a java.lang.​NoSuchMeth​odError when using a java.nio.D​irectByteB​uffer

I created a java library which I need to use in Matab. I narrowed it down to the following code.
My java class:
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public class MyClass {
public void test() {
ByteBuffer bb = ByteBuffer.allocateDirect(100).order(ByteOrder.nativeOrder());
System.out.println(bb.getClass().getName());
bb.flip();
}
}
When using the class in Matlab as follows:
>> import MyClass
>> a = MyClass;
>> a.test
I get the following output:
java.nio.DirectByteBuffer
Java exception occurred:
java.lang.NoSuchMethodError: java.nio.ByteBuffer.flip()Ljava/nio/ByteBuffer;
at MyClass.test(MyClass.java:13)
Since Buffer has the flip method and is a superclass of DirectByteBuffer, DirectByteBuffer should definitely have the method. The java code works when I run it on a regular JVM outside Matlab.

7 Comments

Do you get the same error if you call the java methods directly at matlab's command prompt?
I don't get an error:
>> bb = java.nio.ByteBuffer.allocate(100)
bb =
java.nio.HeapByteBuffer[pos=0 lim=100 cap=100]
>> bb = bb.order(java.nio.ByteOrder.nativeOrder)
bb =
java.nio.HeapByteBuffer[pos=0 lim=100 cap=100]
>> bb.flip
ans =
java.nio.HeapByteBuffer[pos=0 lim=0 cap=100]
-----------------------------------------------------------------------------------------------------
MATLAB Version: 9.6.0.1174912 (R2019a) Update 5
MATLAB License Number: xxx
Operating System: Microsoft Windows 10 Enterprise Version 10.0 (Build 18362)
Java Version: Java 1.8.0_181-b13 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
Hi Guillaume,
Thanks for looking into this. I tried doing it on matlab's command prompt and there I do not get the error. So the problem only occurs when I use the imported java class, I really need that option to work.
I narrowed it down some more, which might hint to a solution.
When I 'help' the Matlab JVM by first casting the ByteBuffer to a Buffer I do not get an exception. My java code then looks like this:
((Buffer)ByteBuffer.allocate(100)).flip();
But when I remove the cast like below the exception occurs again:
ByteBuffer.allocate(100).flip();
In a real java JVM I do not have to cast the reference. Why does the Matlab java implementation not conform to the standard? Is there an option to start Matlab with a proper java implementation?
The java library I use is quite extensive and used for many other purposes, so it is not an option to 'polute' the java code to be able to execute it in Matlab.
Unfortunately, I don't know enough about java to help you there and there's not many java experts on this forum. You may want to contact mathworks directly for support.
I would have thought that the same VM is used for the command line and jar files and in any case it wouldn't be a compliant VM if ByteBuffer didn't inherit from Buffer, so very puzzling.
If you do find the cause of the problem, then please post it as answer.
I cannot reproduce the error you got.
a.test
works fine in MATLAB and shows the following without an error.
java.nio.DirectByteBuffer
I've compiled MyClass.java to MyClass.class using JDK 8. Which Java versions are you using?
Hi Kojiro,
Thanks! Your question provided me with the solution.
I compiled it with JDK11 with target compatibility JAVA8. Although the resulting jars work with my locally installed JVM8, they didnt with the one provided with Matlab. Once I recompiled everything with the JDK8, it works in Matlab as well. This solution works for me for now. Although I need to build a special Java8 version for Matlab.
When do you expect Java11 support in Matlab?
Yes, as of R2019a, MATLAB's Java (JRE) version is 8. So, you need to use JDK 8. I have also tested to build you java code in JDK 9, but in that case, MATLAB shows an error (Import argument 'MyClass' cannot be found or cannot be imported.).
I'm not sure when Java 11 will be supported. But there is a way to change Java version from that of built-in as described in this answer, so you might use Java 11.
Also, for house keeping, could you post your answer by yourself and accept it?

Sign in to comment.

 Accepted Answer

The problem was triggered by compiling with "openjdk version 11.0.3". Although it was compiled with target Java8, and did run under a 'regular' JVM8 outside Matlab, it did not run under the JVM of Matlab. After compiling it with a Java8 JDK it did run in Matlabs JVM. So there seems to be a compatability problem with the Matlab JVM. Building it with a JDK8 solves the problem.

More Answers (0)

Categories

Products

Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!