Java NoClassDefFoundError: com/google/common/base/Preconditions,但前提条件在我的运行时类路径中的 .jar 文件中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18014217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:47:48  来源:igfitidea点击:

NoClassDefFoundError: com/google/common/base/Preconditions, but Preconditions is in the .jar file in my runtime classpath

javaguava

提问by Gregory

I have a project that uses ImmutableListand ImmutableSetfrom Guava. It compiles just fine, but at runtime, with the same -cp, it gives this error message:

我有一个项目,用途ImmutableListImmutableSet番石榴。它编译得很好,但在运行时,使用相同的-cp,它会给出以下错误消息:

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Preconditions
    at com.google.common.collect.RegularImmutableList.get(RegularImmutableList.java:81)
    at BasicPoint.pointAsArray(BasicPoint.java:93)
    at BasicPoint.inflate(BasicPoint.java:134)
    at BasicEdgeLength.<clinit>(BasicEdgeLength.java:32)
Caused by: java.lang.ClassNotFoundException: com.google.common.base.Preconditions
    at java.net.URLClassLoader.run(URLClassLoader.java:366)
    at java.net.URLClassLoader.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 4 more

I compile it in the terminal with this command

我用这个命令在终端中编译它

javac -cp '.:/MYPROJECTDIRECTORY/guava-14.0.1.jar'  BasicEdgeLength.java

and it compiles. Then I run it with this command

它编译。然后我用这个命令运行它

java -cp '.:/MYPROJECTDIRECTORY/guava-14.0.1.jar'  BasicEdgeLength

and I get the aforementioned error message. There are otherquestionson stackoverflow about the exact same issue, but the answer is always to download the guava libraries, which I have already done. Indeed, when I poke around in guava-14.0.1.jar, I can see the file com/google/common/base/Preconditions. The fact that I can see in the .jar file the very class mentioned in the error message makes me imagine that I can't solve this problem just by downloading more files. So I'm not sure what to do, and any suggestions that you could offer would be greatly appreciated.

我收到了上述错误消息。关于完全相同的问题,stackoverflow 上还有其他问题,但答案始终是下载番石榴库,我已经这样做了。确实,当我在guava-14.0.1.jar 中闲逛时,我可以看到文件com/google/common/base/Preconditions. 我可以在 .jar 文件中看到错误消息中提到的那个类,这一事实让我想象我无法通过下载更多文件来解决这个问题。所以我不知道该怎么做,如果你能提供任何建议,我们将不胜感激。

I'm including the code from BasicPointbelow, because that's the class that calls RegularImmutableList.get(i), although please be aware that BasicPoint won't compile without AbstractPoint, IntMatrix, Initializer, and possibly some other stuff (and it doesn't contain the main method, which is found in BasicEdgeLength). I'm happy to append the code to some of these other classes if anyone thinks it's necessary.

我是包括代码BasicPoint的下方,因为这是类调用RegularImmutableList.get(i),虽然请注意,BasicPoint不会编译没有AbstractPointIntMatrixInitializer,可能还有一些其他的东西(它不包含的主要方法,这是在发现BasicEdgeLength)。如果有人认为有必要,我很高兴将代码附加到其他一些类中。

Thank you all for your time!

谢谢大家的时间!

/**
*    This class implements a point.
*    It uses a representation as a vector of integers.  
*/

import com.google.common.collect.*;
//import com.google.common.base.*; // same error occurs whether or not I include this line

final public class BasicPoint implements AbstractPoint<BasicPoint, BasicAngle> {

// static variables for all points.
private static final int length = Initializer.N - 1;

private static final IntMatrix ROT = Initializer.ROT;

private static final IntMatrix REF = Initializer.REF;

private static final IntMatrix INFL = Initializer.INFL;

// A vector identifying the point.  
private final ImmutableList<Integer> point;

// Constructor methods.

private BasicPoint(Integer[] vector) {
    if (vector.length != length) {
        throw new IllegalArgumentException("Point length is incorrect.");
    }
    this.point = ImmutableList.copyOf(vector);
}

private BasicPoint() {
    Integer[] vector = new Integer[length];
    for (int i = 0; i < length; i++) {
        vector[i] = 0;
    }
    point = ImmutableList.copyOf(vector);
}

// public static factory method
static public BasicPoint createBasicPoint(Integer[] vector) {
    return new BasicPoint(vector);
}

static final public BasicPoint zeroVector() {
    return new BasicPoint();
}

static final public BasicPoint unitVector() {
    Integer[] vector = new Integer[length];
    vector[0] = 1;
    for (int i = 1; i < length; i++) {
        vector[i] = 0;
    }
    return new BasicPoint(vector);
}

// toString method.
public String toString() {
    String outString = "(";
    for (int i = 0; i < point.size() - 1; i++) {
        outString = outString + point.get(i) + ",";
    }
    outString = outString + point.get(length) + ")";
    return outString;
}

// equals method.
public boolean equals(Object obj) {
    if (obj == null || getClass() != obj.getClass())
        return false;
    BasicPoint p = (BasicPoint) obj;
    for (int i = 0; i < length; i++) {
        if (p.point.get(i) != this.point.get(i))
            return false;
    }
    return true;
}

// hashCode override.
public int hashCode() {
    int prime = 53;
    int result = 11;
    for (int i = 0; i < length; i++) {
        result = prime*result + point.get(i);
    }
    return result;
}

// a private helper method to turn point into an array of Integers.
private Integer[] pointAsArray() {
    Integer[] output = new Integer[length];
    for (int i = 0; i < length; i++)
        output[i] = point.get(i);
    return output;
}

// Manipulation methods.  
public BasicPoint add(BasicPoint p) {
    Integer[] q = new Integer[length];
    for (int i = 0; i < length; i++) {
        q[i] = this.point.get(i) + p.point.get(i);
    }
    return new BasicPoint(q);
}

public BasicPoint scalarMultiple(int c) {
    Integer[] q = new Integer[length];
    for (int i = 0; i < length; i++) {
        q[i] = c * this.point.get(i);
    }
    return new BasicPoint(q);
}

public BasicPoint subtract(BasicPoint p) {
    return this.add(p.scalarMultiple(-1));
}

public BasicPoint rotate(BasicAngle a) {
    int i = a.getAsInt();
    if (i < 0)
        throw new IllegalArgumentException("You must perform a positive number of rotations.");

    Integer[] result  = new Integer[length];
    for (int j = 0; j < i; j++)
        result = ROT.rowTimes(result);
    return new BasicPoint(result);
}

public BasicPoint reflect() {
    return new BasicPoint(REF.rowTimes(this.pointAsArray()));
}

public BasicPoint inflate() {
    return new BasicPoint(INFL.rowTimes(this.pointAsArray()));
}

} // end of class BasicPoint

回答by Gregory

A friend took a look at the files in my project and identified the problem for me. He noticed that my version of guava had a bad checksum. I tried installing it again, and the problem persisted. Both times that I installed it I opened and saved the .jar file to disk using the linux archive manager (which was the default behavior in my web browser). I tried one more time, this time using wget to download the .jar file, and that solved the problem. So the moral of the story appears to be: don't save .jar files to disk with the linux archive manager.

一位朋友查看了我项目中的文件,并为我找出了问题所在。他注意到我的番石榴版本有一个错误的校验和。我再次尝试安装它,问题仍然存在。两次安装它时,我都使用 linux 存档管理器打开了 .jar 文件并将其保存到磁盘(这是我的 Web 浏览器中的默认行为)。我又试了一次,这次使用wget下载.jar文件,问题解决了。所以这个故事的寓意似乎是:不要使用 linux 存档管理器将 .jar 文件保存到磁盘。