4

我有一个使用ImmutableListImmutableSet来自 Guava 的项目。它编译得很好,但在运行时,使用相同的-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$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.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

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

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

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

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

我收到上述错误消息。stackoverflow 上还有其他 关于完全相同的问题的问题,但答案总是下载我已经完成的 guava 库。确实,当我在guava-14.0.1.jar中四处寻找时,我可以看到该文件com/google/common/base/Preconditions。我可以在 .jar 文件中看到错误消息中提到的类,这一事实让我想象我不能仅仅通过下载更多文件来解决这个问题。因此,我不确定该怎么做,您可以提供的任何建议将不胜感激。

我包含了BasicPoint下面的代码,因为那是调用的类RegularImmutableList.get(i),尽管请注意 BasicPoint 不会在没有AbstractPoint, IntMatrix,Initializer以及可能的其他一些东西的情况下编译(并且它不包含 main 方法,该方法位于BasicEdgeLength)。如果有人认为有必要,我很乐意将代码附加到其他一些类中。

谢谢大家的时间!

/**
*    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
4

1 回答 1

4

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

于 2013-08-15T17:45:31.013 回答