简体   繁体   中英

Cloning a repository to a local directory with JGit

I am working on a project which involves cloning a git repository to a local directory.

I read through the tutorial and my code looks like this:

/*
 * Clones a github project to the designated directory and checks it out. (Both prefix and postfix)
 * <p>
 * @param buildPath - String, the path where the directories will be created and the projects built.
 * @param bugID - String, the bug's id.
 * @param projectURL - String, the github URL used to clone the project.
 */
public void DownloadAndSetup(String buildPath, String bugID, String projectURL, String prefixHash, String postfixHash) throws InvalidRemoteException, TransportException, GitAPIException {
    String generalPath = buildPath+File.separator+bugID;
    String prefixPath = generalPath+File.separator+"prefix";
    String postfixPath = generalPath+File.separator+"postfix";
    // Cloning & checking out to prefix
    Git gitPrefix = Git.cloneRepository().setURI(projectURL).setDirectory(new File(prefixPath)).call();
    gitPrefix.checkout().setCreateBranch(true).setName("new-branch").setStartPoint(prefixHash).call();
    // Cloning & checking out to postfix
    Git gitPostfix = Git.cloneRepository().setURI(projectURL).setDirectory(new File(postfixPath)).call();
    gitPostfix.checkout().setCreateBranch(true).setName("new-branch").setStartPoint(postfixHash).call();
}

But when I run:

public static void main(String[] args) throws InvalidRemoteException, TransportException, GitAPIException {
    GitDownloader gd = new GitDownloader();
    String buildPath = "C:\\Users\\dario_000\\Dropbox\\SheffInternship\\wsp\\AutoEv\\src\\tests";
    gd.DownloadAndSetup(buildPath, "download", "https://github.com/jhy/jsoup.git", "1e9ae842ca94f326215358917c620ac407323c81", "04e256ce9571e4239403b657126ce8eb30ad6776");
}

I get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: com/jcraft/jsch/JSchException
    at org.eclipse.jgit.transport.Transport.<clinit>(Transport.java:114)
    at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:120)
    at org.eclipse.jgit.api.CloneCommand.fetch(CloneCommand.java:178)
    at org.eclipse.jgit.api.CloneCommand.call(CloneCommand.java:125)
    at autoEvoSuite.GitDownloader.DownloadAndSetup(GitDownloader.java:26)
    at tests.GITProjectDownloader.main(GITProjectDownloader.java:19)
Caused by: java.lang.ClassNotFoundException: com.jcraft.jsch.JSchException
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 6 more

Anybody got any suggestions?

JGit has a dependency on JSch and JavaEWAH and apparently JSch is not on your runtime classpath.

You will have to either manually add the required libraries to the classpath or have the classpath managed by eg Maven or OSGi (Tools).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM