简体   繁体   English

仅在树莓派上出现java.lang.NoSuchMethodError

[英]java.lang.NoSuchMethodError on raspberry pi only

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. 我正在运行Eclipse Kepler Service Release2。我的程序在Eclipse中运行时可以正常运行,而在使用Windows cmd运行.jar时也可以正常运行。 However, putting that same .jar onto a raspberry pi, I get the following error: 但是,将相同的.jar放在树莓派上,出现以下错误:

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. 我部分地确信问题出在我错误地编译了jar,但由于我是这种事情的新手,所以我不知道如何检查我是否做对了。 Does anyone have any advice? 有人有建议吗?

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. 您正在尝试使用Java 8中引入的java.nio.file.Files.readAllLines(Path) ,而您将无法在Java 7中使用它。

Options: 选项:

  • Upgrade to Java 8 on the raspberry pi 在树莓派上升级到Java 8
  • 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) 不要使用Java 8指定的任何类/方法。(将Eclipse项目更改为以Java 7 JRE为目标可以强制执行此操作)

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. 碰巧的是,Java 7上有带PathCharsetreadAllLines重载, 无论如何,这都是更好的重载,因此您可以清楚地知道要使用哪种编码。 So change your code to: 因此,将您的代码更改为:

// Or whichever Charset you really want...
List<String> lines = Files.readAllLines(p, StandardCharsets.UTF_8);

Rasberry Pi可能具有Java ME(微型版),它不包含Java SE(标准版)中运行Eclipse的计算机上可能具有的许多方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM