Reputation: 19
I get this runtime error and don't know how to fix:
java.lang.NoSucheMethodError: java.io.FileInputStream.transferTo(Ljava/io/OutputStream;)J
This happens while using the transferTo method from a FileInputStream to a ZipOutputStream
FileInputStream fis = new FileInputStream(file);
ZipEntry zipEntry = new ZipEntry(fileName);
zos.putNextEntry(zipEntry);
try {
fis.transferTo(zos);
} catch (NoSuchMethodError e) {
e.printStackTrace();
}
Upvotes: 0
Views: 3540
Reputation: 697
Multiple reasons for occuring NoSuchMethodError
1) java.lang.NoSuchMethodError is being thrown when program tries to call a class method that doesn’t exist. The method can be static or it can be an instance method too.
2) Conflict because of same class name. For example, your application is using some third party jar which has same fully qualified class name as yours. So if classloader loads the class from other jar, you will get java.lang.NoSuchMethodError at runtime.
3) Jar version used at the compile time is different from the runtime. For example, you might have MySQL jar in your application having different version from what is present in Tomcat lib folder. Since tomcat lib folder jars are looked first by Java Classloader, there is a chance of java.lang.NoSuchMethodError if any method is not found in the class loaded.
Upvotes: 1
Reputation: 140457
Simple: you compiled your code against Java 9 (as that method was added with java 9, which you can see in the javadoc), but it seems you are running it against an older java runtime.
That exception always tells you that the dependencies you used at compile do not match your run time setup. That is all there is to this.
Upvotes: 6