I am trying to clone a git repo locally through the java code that I have. And this is how it looks like:
class Scratch {
public static void main(String[] args) {
String repoName = "https://github.somedomain.co/AppPipelines/some-project.git";
String username = "username";
String personalAccessToken = "some_personal_access_token";
String repoUrl = "https://github.somedomain.co/AppPipelines/some_repo";
String cloneDirectoryPath = "/Users/username/DSS/1223";
try {
// Create credentials provider with username and token
UsernamePasswordCredentialsProvider credentialsProvider =
new UsernamePasswordCredentialsProvider("token", personalAccessToken);
Git.cloneRepository()
.setURI(repoUrl)
.setDirectory(new File(cloneDirectoryPath))
.setBranch("main")
.setCredentialsProvider(credentialsProvider)
.call();
} catch (GitAPIException e) {
System.err.println("Error cloning repository: " + e.getMessage());
e.printStackTrace();
}
}
}
When I try to run this code, I get this exception:
Error cloning repository: git@github.bamtech.co:AppPipelines/nexus-rm: remote hung up unexpectedly
org.eclipse.jgit.api.errors.TransportException: git@github.bamtech.co:AppPipelines/nexus-rm: remote hung up unexpectedly
at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:249)
at org.eclipse.jgit.api.CloneCommand.fetch(CloneCommand.java:319)
at org.eclipse.jgit.api.CloneCommand.call(CloneCommand.java:189)
at Scratch.main(scratch_1.java:27)
Caused by: org.eclipse.jgit.errors.TransportException: git@github.bamtech.co:AppPipelines/nexus-rm: remote hung up unexpectedly
at org.eclipse.jgit.transport.TransportGitSsh$SshFetchConnection.<init>(TransportGitSsh.java:317)
at org.eclipse.jgit.transport.TransportGitSsh.openFetch(TransportGitSsh.java:152)
at org.eclipse.jgit.transport.FetchProcess.executeImp(FetchProcess.java:153)
at org.eclipse.jgit.transport.FetchProcess.execute(FetchProcess.java:105)
at org.eclipse.jgit.transport.Transport.fetch(Transport.java:1458)
at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:238)
... 3 more
Caused by: java.lang.NullPointerException: Cannot invoke "org.eclipse.jgit.transport.SshSessionFactory.getSession(org.eclipse.jgit.transport.URIish, org.eclipse.jgit.transport.CredentialsProvider, org.eclipse.jgit.util.FS, int)" because "this.sch" is null
at org.eclipse.jgit.transport.SshTransport.getSession(SshTransport.java:107)
at org.eclipse.jgit.transport.TransportGitSsh$SshFetchConnection.<init>(TransportGitSsh.java:285)
... 8 more
Here are the Gradle plugins and dependencies:
plugins {
id 'org.springframework.boot' version '3.4.1'
id 'io.spring.dependency-management' version '1.1.0'
id 'java'
id 'jacoco'
id 'com.diffplug.spotless' version '6.20.0'
}
dependencies {
implementation "org.springframework.boot:spring-boot-starter-web"
implementation "org.springdoc:springdoc-openapi-starter-webmvc-ui:${swaggerVersion}"
testImplementation "org.springframework.boot:spring-boot-starter-test"
compileOnly "org.projectlombok:lombok:${lombokVersion}"
annotationProcessor "org.projectlombok:lombok:${lombokVersion}"
implementation "org.springframework.boot:spring-boot-starter-validation"
implementation "org.eclipse.jgit:org.eclipse.jgit:${eclipseJgitVersion}"
implementation "org.eclipse.jgit.ssh.jsch:${eclipseJgitVersion}"
implementation group: 'com.jcraft.jsch', name: 'com.jcraft.jsch', version: '0.1.37-20081006'
implementation "com.google.code.gson:gson:${gsonVersion}"
implementation platform("software.amazon.awssdk:bom:${awsSdk}")
implementation 'software.amazon.awssdk:dynamodb'
implementation 'software.amazon.awssdk:dynamodb-enhanced'
implementation "com.github.derjust:spring-data-dynamodb:${springDataDynamoDB}"
implementation "com.amazonaws:aws-java-sdk-sts:${awsSdk1Sts}"
implementation "com.datadoghq:datadog-api-client:${ddClient}"
implementation 'software.amazon.awssdk:sts'
implementation 'software.amazon.awssdk:eks'
implementation "software.amazon.awssdk:batch:${awsSdk}"
implementation "software.amazon.awssdk:autoscaling:${awsSdk}"
implementation "software.amazon.awssdk:netty-nio-client:${awsSdk}"
implementation "software.amazon.awssdk:ec2:${awsSdk}"
implementation "software.amazon.awssdk:ecs:${awsSdk}"
implementation "com.datadoghq:java-dogstatsd-client:${dataDogStatSd}"
implementation "org.springframework.boot:spring-boot-starter-actuator"
implementation 'org.springframework.boot:spring-boot-starter-aop'
implementation 'io.micrometer:micrometer-registry-datadog:1.14.3'
implementation "org.springframework.security:spring-security-web:${springSecurityWebVersion}"
implementation "com.disneystreaming.nexus:common-java-libs:0.0.2"
}
And the version is eclipseJgitVersion=7.1.0.202411261347-r.
I am not sure if I am missing anything here. This happens only with a certain domain because, I am able to clone repositories from other domains without any issue with corresponding usernames and tokens. Let me know if this is missing anything. Thanks in advance.
In your case, the error is actually caused by CredentialsProvider
not configured correctly. The personal access token should be passed in place of the username, while the password should be empty.
UsernamePasswordCredentialsProvider credentialsProvider =
new UsernamePasswordCredentialsProvider(personalAccessToken, "");
Instead, for SSH connections (and properly configured credentials), the issue might be caused by the defaut SSH factory. In fact, according to this Eclipse Wiki, since version 5.8.x
, the default SSH factory jsch
has been moved from jgit
to org.eclipse.jgit:org.eclipse.jgit.ssh.jsch
. Therefore, you need to add the corresponding dependency to make it work.
implementation group: 'org.eclipse.jgit', name: 'org.eclipse.jgit.ssh.jsch', version: '(version)'
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit.ssh.jsch</artifactId>
<version>${jgit.version}</version>
</dependency>
The issue was also reported in the following GitHub repositories: issue 20 and issue 540.
Keep in mind that org.eclipse.jgit.ssh.jsch
also relies on com.jcraft.jsch
. As of March 2025, the only version still maintained that I could find is this fork under com.github.mwiede
.