How to download a Git repository using Java in Eclipse

In this tutorial, you will learn how to download a Git repository using a Java program in Eclipse.

The Eclipse version used in this tutorial is Eclipse IDE for Java Developers, 2021-09 (4.21.0).

You can use the JGit Java library to carry out this task. It is a pure implementation of the Git version control system. The latest version of JGit is available in the Maven Repository. You need to convert the project to the Maven project and integrate the JGit API as a Maven Dependency.

Downloading a Git repository using Java:

Steps to integrate the JGit library:

  1. Open Eclipse, and create a new Java project.
  2. Right-click on the project, go to configure and click on convert to Maven Project.
    Right-click -> Configure -> Convert to Maven Project -> Finish.
  3. Once this is done, you should see an auto-generated pom.xml file under the target folder.
  4. Add the following dependency in the pom.xml file to integrate JGit with your project.
    <dependency> 
    <groupId>org.eclipse.jgit</groupId> 
    <artifactId>org.eclipse.jgit</artifactId> 
    <version>4.6.0.201612231935-r</version> 
    </dependency>
    

    Add the above code within the <dependencies> </dependencies> tags, after the </build> tag. If the <dependencies> </dependencies> tags aren’t already in the file, add them and insert the above code within the tags.
    After adding the dependency, save the file, and the Maven Dependency folder will get created in the project.
    In case there is an error during this process, try updating the Maven project. To do this, right-click on the project -> Maven -> Update project -> Check all the boxes -> Click OK. Or, as a shortcut, you can press Alt + F5.

  5. The error ‘Failed to load class org.slf4j.impl.StaticLoggerBinder’ may occur. This happens when there is no appropriate SLF4J binding found on the class path. To solve this, download the jar files, slf4j-simple.jar and slf4j-api.jar, and place them on the class path. You can download them from the Maven Repository, or use the links given below (Click on ‘jar’ in ‘Files’ and the download will begin).
    https://mvnrepository.com/artifact/org.slf4j/slf4j-api/2.0.0-alpha5
    https://mvnrepository.com/artifact/org.slf4j/slf4j-simple/2.0.0-alpha5
    To place the jar files into the class path, follow the steps given below.
    Right-click on the project -> Properties -> Java Build Path -> Libraries -> Add External JARs -> Select the downloaded JAR files -> Apply and Close.
  6. Create a Java class in the project and run the code given below.

Java program guide:

  1. Import the required classes from the JGit library as shown in the program, and implement the code in a try-catch block to catch the exceptions.
  2. You can configure the JGit commands through method chaining.
  3. Use the cloneRepository() method to clone the required repository.
  4. Pass the repository link as a parameter to the setURI() method. Click on ‘Code’ on GitHub to get the repository link as shown:
    Download Git repository in Java Program
  5. Pass the location of the directory as a parameter to the setDirectory() method. The location should be the location of the directory into which you want to download the repository. This directory should be empty and unique. If it is not, it will throw a JGitInternalException.
  6. Finally, use the call() method to start the execution.
  7. The required repository will be downloaded in the given directory.

Java Program to download Git Repository

import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.JGitInternalException;
import java.io.File;
import org.eclipse.jgit.api.Git;

public class gitRepository {
   public static void main(String[] args) {
     try {
       //Provide the correct Repository link and the directory to store the repository on the computer. 
       String repositoryLink = "https://github.com/smitha16/Hangman.git";
       String directory = "D:\\GitRepos\\Hangman";
       Git.cloneRepository().setURI(repositoryLink).setDirectory(new File(directory)).call();
       System.out.println("Successfully downloaded the repository!");
     }
     catch(GitAPIException | JGitInternalException e) {
       System.out.println(e.getMessage());
       e.printStackTrace();
     }
   }
}

Output:

Successfully downloaded the repository!

In the program above, you are downloading the Hangman repository shown in the fourth step of the Java program guide, and storing it in a folder called Hangman, which is in the GitRepos folder of D drive. After running the program, the repository will get downloaded into the specified directory on the computer as shown below.

Java Program to download Git Repository

Hope this tutorial helped. Happy coding!

Also read, What is GitHub and How to push your code on GitHub ?

Leave a Reply

Your email address will not be published. Required fields are marked *