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

조회 수: 12 (최근 30일)
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
Dennis van den Berg
Dennis van den Berg 2019년 9월 4일
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?
Kojiro Saito
Kojiro Saito 2019년 9월 4일
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?

댓글을 달려면 로그인하십시오.

채택된 답변

Dennis van den Berg
Dennis van den Berg 2019년 9월 4일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Java Package Integration에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by