主要问题/TLDR:我的调用堆栈中的某个方法出现了 NoSuchMethodError 错误。但是在同一项目中我自己的主函数中调用该方法没有给出错误。怎么会这样呢?我可以尝试哪些技术来缩小此类错误的来源范围?
其他事实:文档显示该方法存在。引发错误的源具有正确的导入并尝试调用该方法。错误打印输出中的方法名称后面有一些奇怪的官样文章。这会是问题所在吗?
我的案例的具体信息:
完整错误打印输出:
Exception in thread "main" java.lang.NoSuchMethodError: java.nio.ByteBuffer.clear()Ljava/nio/ByteBuffer;
at it.unimi.dsi.sux4j.io.ChunkedHashStore.reset(ChunkedHashStore.java:526)
at it.unimi.dsi.sux4j.mph.GOVMinimalPerfectHashFunction.<init>(GOVMinimalPerfectHashFunction.java:335)
at it.unimi.dsi.sux4j.mph.GOVMinimalPerfectHashFunction$Builder.build(GOVMinimalPerfectHashFunction.java:270)
at org.boydwebb.familysearch.runners.examples.ManyGraphWriter.<init>(ManyGraphWriter.java:109)
at org.boydwebb.familysearch.runners.examples.ManyGraphWriter.main(ManyGraphWriter.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.hadoop.util.ProgramDriver$ProgramDescription.invoke(ProgramDriver.java:68)
at org.apache.hadoop.util.ProgramDriver.driver(ProgramDriver.java:139)
at org.boydwebb.familysearch.runner.Runner.run(Runner.java:62)
at org.boydwebb.familysearch.runner.Runner.main(Runner.java:23)
相关来源:(https://raw.githubusercontent.com/vigna/Sux4J/master/src/it/unimi/dsi/sux4j/io/ChunkedHashStore.java)
我插入 main 的行是为了尝试获得一个最小的工作示例:
ByteBuffer buffer = ByteBuffer.allocateDirect(100);
buffer.clear();
我的原始代码中的行产生了错误:
import it.unimi.dsi.sux4j.mph.GOVMinimalPerfectHashFunction;
Long[] table = \*initialize here*\
TransformationStrategy<Long> transformationStrategy = TransformationStrategies.fixedLong();
GOVMinimalPerfectHashFunction.Builder<Long> builder = new GOVMinimalPerfectHashFunction.Builder<>();
builder.keys(Arrays.asList(table));
builder.transform(transformationStrategy);
builder.store(null);
builder.tempDir(null);
builder.signed(0);
toGiantIdx = builder.build();//ERROR HERE
最佳答案
预编译库字节码包含对内部命名为java.nio.ByteBuffer.clear()Ljava/nio/ByteBuffer;
的方法的引用,这意味着在java.nio中定义的方法.ByteBuffer
(可能在派生类中重写)带有声明
java.nio.ByteBuffer clear();
此请求的方法自 Java SE 9 起就存在。如果使用较旧的 Java 运行时执行代码,则会发生此错误。
如果您自己的代码是使用较旧的 JDK 编译的,则编译器会将 buffer.clear()
解析为指向 java.nio.Buffer java.nio.Buffer.clear()
方法(自 JDK 1.4 起就存在)。
关于java - 一个方法怎么可能存在于程序的一个部分而另一个部分不存在呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52978157/