First of all: i know that there are a lots of questions on this topic but what's happening to me it's quite different from the standard and i'm not able to fix it.
Now I'm using Eclipse, and I'm using 2 external libreries: json and jgit.
The problem is: json works, but jgit doesn't, why?
What i get when i run my code is
`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
`
Reading on the internet I realized that this error happens because the JVM at runtime cannot find the class I am using, however I don't understand how to fix the problem. The strange thing is that in another class I use the json library and that doesn't give me any problems.
For both libraries what I did was go to "Configure Build Path", add JARs to "Modulepath" and add the paths to these JARs as variables in "Classpath".
This is my code, if you need it.
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();
}
}
Does anyone know how to fix this?
Straight forward, the error is coming due to missing slf4j dependencies.
If it's a maven project, then add these dependency in pom.xml :
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<!-- This is used for slf4j binding with log4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>
Or add the jars on the classpath by downloading from links below.
Slf4j API - https://repo1.maven.org/maven2/org/slf4j/slf4j-api/1.7.25/slf4j-api-1.7.25.jar
Slf4j-Log4j12 - https://repo1.maven.org/maven2/org/slf4j/slf4j-log4j12/1.7.25/slf4j-log4j12-1.7.25.jar