I get a java.lang.NoSuchMethodError when using a java.nio.DirectByteBuffer
显示 更早的评论
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 个评论
Guillaume
2019-8-30
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
Dennis van den Berg
2019-8-30
编辑:Dennis van den Berg
2019-8-30
Dennis van den Berg
2019-8-30
编辑:Dennis van den Berg
2019-9-2
Guillaume
2019-9-2
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.
Kojiro Saito
2019-9-3
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?
Dennis van den Berg
2019-9-4
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?
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Java Package Integration 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!