问题
I'm running Eclipse Kepler Service Release 2. My program works fine when I run it in Eclipse, and it also works fine when I run the .jar using windows cmd. However, putting that same .jar onto a raspberry pi, I get the following error:
Exception in thread "Thread-1" java.lang.NoSuchMethodError: java.nio.file.Files.readAllLines(Ljava/nio/file/Path;)Ljava/util/List;
The bits of code in question are
import java.nio.file.Files;
import java.nio.file.Path;
import dataTypes.Detection;
public final class FileOperations {
// ...
public static Detection readDetection(Path p) {
try {
List<String> lines = Files.readAllLines(p);
// etc ...
I'm partially convinced that the problem lies with my having incorrectly compiled the jar, but since I'm a complete novice at this sort of thing I don't know how to check I'm doing it right. Does anyone have any advice?
回答1:
You're trying to use java.nio.file.Files.readAllLines(Path), which was introduced in Java 8. You're not going to be able to use that in Java 7.
Options:
- Upgrade to Java 8 on the raspberry pi
- Don't use any classes/methods which are specified to Java 8. (Change your Eclipse project to target a Java 7 JRE to enforce this)
As it happens, the overload of readAllLines
which takes a Path
and a Charset
is available on Java 7, and that's a better overload to use anyway, so that you're explicit about which encoding you want to use. So change your code to:
// Or whichever Charset you really want...
List<String> lines = Files.readAllLines(p, StandardCharsets.UTF_8);
回答2:
Rasberry Pi probably has Java ME (Micro Edition), which does not contain many of the methods in Java SE (Standard Edition) which you probably have on your machine running Eclipse.
来源:https://stackoverflow.com/questions/23957425/java-lang-nosuchmethoderror-on-raspberry-pi-only