gpt4 book ai didi

java - NoClassDefFoundError : com/google/common/base/Preconditions, 但先决条件在我的运行时类路径中的 .jar 文件中

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:28:31 26 4
gpt4 key购买 nike

我有一个使用 Guava 的 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$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

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

我在下面包含了 BasicPoint 的代码,因为这是调用 RegularImmutableList.get(i) 的类,但请注意 BasicPoint 不会编译没有 AbstractPointIntMatrixInitializer 和可能的其他一些东西(并且它不包含 main 方法,可以在 基本边长)。如果有人认为有必要,我很乐意将代码附加到其他一些类中。

谢谢大家的宝贵时间!

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

最佳答案

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

关于java - NoClassDefFoundError : com/google/common/base/Preconditions, 但先决条件在我的运行时类路径中的 .jar 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18014217/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com