【发布时间】:2020-11-01 12:47:45
【问题描述】:
首先:我知道关于这个主题有很多问题,但是发生在我身上的事情与标准完全不同,我无法解决它。
现在我正在使用 Eclipse,并且我正在使用 2 个外部库:json 和 jgit。
问题是:json可以用,jgit不行,为什么?
当我运行我的代码时我得到的是
`Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at org.eclipse.jgit.util.SystemReader.<clinit>(SystemReader.java:55)
at org.eclipse.jgit.lib.BaseRepositoryBuilder.readEnvironment(BaseRepositoryBuilder.java:363)
at org.eclipse.jgit.api.InitCommand.call(InitCommand.java:58)
at org.eclipse.jgit.api.CloneCommand.init(CloneCommand.java:267)
at org.eclipse.jgit.api.CloneCommand.call(CloneCommand.java:173)
at GitHub.GitHubAPI.init(GitHubAPI.java:35)
at GitHub.GitHubAPI.main(GitHubAPI.java:73)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 7 more
`
在互联网上阅读我意识到发生此错误是因为 JVM 在运行时找不到我正在使用的类,但是我不明白如何解决这个问题。 奇怪的是,在另一个类中我使用了 json 库,这并没有给我带来任何问题。
对于这两个库,我所做的是转到“配置构建路径”,将 JAR 添加到“模块路径”,并将这些 JAR 的路径作为变量添加到“类路径”中。
这是我的代码,如果你需要的话。
package GitHub;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
//import java.util.logging.Logger;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ListBranchCommand.ListMode;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.InvalidRemoteException;
import org.eclipse.jgit.api.errors.TransportException;
import org.eclipse.jgit.lib.Ref;
//import org.eclipse.jgit.revwalk.RevCommit;
public class GitHubAPI {
// private static Logger myLogger = Logger.getLogger("InfoLogging");
private String projNameMin;
private final String url = "https://github.com/apache/";
private Git git;
private String repoLocalPath;
public GitHubAPI(String projectName) {
this.projNameMin = projectName.toLowerCase();
this.repoLocalPath = "./"+projNameMin+"Repo";
}
public void init() {
String uri = url + projNameMin + ".git";
try {
git = Git.cloneRepository().setURI(uri).setDirectory(new File(repoLocalPath)).call(); //this line triggers the error
} catch (InvalidRemoteException e) {
e.printStackTrace();
} catch (TransportException e) {
e.printStackTrace();
} catch (GitAPIException e) {
e.printStackTrace();
}
}
public ArrayList<GitCommit> getCommits(){
ArrayList<GitCommit> commits = new ArrayList<GitCommit>();
// Iterable<RevCommit> commitsLog = null;
try {
// boolean head = false;
List<Ref> branches = git.branchList().setListMode(ListMode.ALL).call();
for (Ref branch: branches) {
String branchName = branch.getName();
System.out.println(branchName);
if (branchName.startsWith("refs/heads/")) {
// head = true;
}
}
} catch (GitAPIException e) {
e.printStackTrace();
}
return commits;
}
public static void main(String[] args) {
GitHubAPI githubapi = new GitHubAPI("VCL");
githubapi.init();
githubapi.getCommits();
}
}
有谁知道如何解决这个问题?
【问题讨论】:
-
@Abra 好的,但我不明白该怎么做。此外,在 Modulepath 实体和 Classpath 实体中,还有 JAR 和配置模块路径中的变量
标签: java eclipse noclassdeffounderror classnotfoundexception