Anonymous
Вызвано: java.lang.NoClassDefFoundError: org/kohsuke/github/GHFileNotFoundException
Сообщение
Anonymous » 08 май 2024, 23:50
Я без проблем запускаю проект в IntelijIdea. Когда я пытаюсь запустить такие команды:
Установите свой токен личного доступа GitHub как переменную среды с именем GITHUB_TOKEN.
Соберите приложение с помощью команды mvn clean install.
После успешной сборки будет создан файл github-analyzer.jar. создается в целевом каталоге.
Перейдите в целевой каталог с помощью команды cd target.
Запустите приложение с помощью команды java -jar github-analyzer-1.0-SNAPSHOT.jar.`
maven запускается, но в при последнем вызове я получаю эту ошибку:
Ошибка: невозможно инициализировать основной класс GitHubAnalyzer
Причина: java.lang.NoClassDefFoundError:
org/kohsuke/github/GHFileNotFoundException
Это мой код, я делаю что-то не так?
Код: Выделить всё
import org.kohsuke.github.*;
import io.github.cdimascio.dotenv.Dotenv;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
public class GitHubAnalyzer {
public static String GITHUB_TOKEN;
static {
Dotenv dotenv = Dotenv.load();
GITHUB_TOKEN = dotenv.get("GITHUB_TOKEN");
}
public static final long WEEK_IN_MILLIS = 7L * 24 * 3600 * 1000;
public static void main(String[] args) throws IOException {
if(GITHUB_TOKEN.isEmpty()) {
System.err.println("Please set the GITHUB_TOKEN environment variable");
return;
}
try (Scanner scanner = new Scanner(System.in)) {
System.out.println("GitHub Analyzer");
System.out.println("Insert owner:");
String owner = scanner.next();
System.out.println("Insert repository:");
String repository = scanner.next();
System.out.println("What do you want to analyze?");
System.out.println("1. Pair of contributors that worked on the same file");
System.out.println("2. Contributions of contributors in the last week");
int choice = scanner.nextInt();
GitHub github;
try {
github = new GitHubBuilder().withOAuthToken(GITHUB_TOKEN).build();
}catch (IOException e){
System.err.println("An error occurred while accessing GitHub: " + e.getMessage());
return;
}
analyzeRepository(github, owner, repository, choice);
} catch (IOException e) {
System.err.println("An error occurred while analyzing the repository: " + e.getMessage());
}
}
private static void analyzeRepository(GitHub github, String owner, String repository, int choice) throws IOException {
GHRepository repo;
try {
repo = github.getRepository(owner + "/" + repository);
} catch (GHFileNotFoundException e) {
System.out.println("Repository not found");
return;
}catch (IOException e){
System.out.println("An error occurred while accessing the repository: " + e.getMessage());
return;
}
List commits = new ArrayList();
try {
commits = repo.listCommits().withPageSize(100).toList();
}catch (IOException e){
System.out.println("An error occurred while accessing the repository: " + e.getMessage());
return;
}
int numOfCommits = commits.size();
Map commitsByAuthor = commits.stream()
.filter(commit -> {
try {
return commit.getAuthor() != null;
} catch (IOException e) {
throw new RuntimeException(e);
}
}).collect(Collectors.groupingBy(commit -> {
try {
return commit.getAuthor().getLogin();
} catch (IOException e) {
throw new RuntimeException(e);
}
}));
List contributors = repo.listContributors().toList();
Date weekAgo = new Date(System.currentTimeMillis() - WEEK_IN_MILLIS);
AtomicInteger counter = new AtomicInteger(0);
Map contributorsFileMap = new ConcurrentHashMap();
if (choice == 1) {
analyzeContributorsPairs(repo, commits, numOfCommits, counter, contributorsFileMap, contributors);
} else if(choice == 2){
analyzeContributorsContributions(repo, commitsByAuthor, weekAgo, counter, contributors);
}
}
private static void analyzeContributorsContributions(GHRepository repo, Map commitsByAuthor, Date weekAgo, AtomicInteger counter, List contributors) throws IOException {
System.out.println("Analyzing contributors contributions");
for(GHRepository.Contributor contributor:contributors){
List commitsByThisAuthor = commitsByAuthor.getOrDefault(contributor.getLogin(),Collections.emptyList());
analyzeContributor(repo, contributor, weekAgo,commitsByThisAuthor);
}
}
private static void analyzeContributorsPairs(GHRepository repo, List commits, int numOfCommits, AtomicInteger counter, Map contributorsFileMap, List contributors) {
System.out.println("Analyzing contributors pairs");
commits.forEach((commit)-> parseCommit(commit, counter, numOfCommits, contributorsFileMap));
displayResult(getTopPairs(contributorsFileMap));
}
private static void analyzeContributor(GHRepository repo, GHRepository.Contributor contributor, Date weekAgo, List commitsByThisAuthor) throws IOException {
System.out.println("Analyzing contributor: " + contributor.getLogin());
int linesOfCode = 0;
int commitsNo;
int mergeRequests;
int maxCharsInComment = 0;
List commits = commitsByThisAuthor.stream()
.filter(commit -> {
try {
return commit.getCommitDate().after(weekAgo);
} catch (IOException e) {
throw new RuntimeException(e);
}
})
.collect(Collectors.toList());
commitsNo = commits.size();
for(GHCommit commit : commits) {
for(GHCommit.File file : commit.getFiles()) {
linesOfCode += file.getLinesAdded();
}
}
List pullRequests = repo.getPullRequests(GHIssueState.ALL)
.stream().filter(ghPullRequest -> {
try {
return (ghPullRequest.getUser().getLogin().equals(contributor.getLogin()) && ghPullRequest.getCreatedAt().after(weekAgo));
} catch (IOException e) {
throw new RuntimeException(e);
}
}).collect(Collectors.toList());
mergeRequests = pullRequests.size();
List comments = new ArrayList();
pullRequests.stream().map(ghPullRequest -> {
try {
return ghPullRequest.listReviewComments();
} catch (IOException e) {
throw new RuntimeException(e);
}
}).forEach(ghPullRequestReviewComments -> {
try {
comments.addAll(ghPullRequestReviewComments.toList());
} catch (IOException e) {
throw new RuntimeException(e);
}
});
maxCharsInComment = comments.stream().
map(GHPullRequestReviewComment::getBody).
map(String::length).max(Integer::compareTo).orElse(0);
System.out.println("Contributor: " + contributor.getLogin());
System.out.println("Lines of code: " + linesOfCode);
System.out.println("Commits: " + commitsNo);
System.out.println("Merge requests: " + mergeRequests);
System.out.println("Max chars in comment: " + maxCharsInComment);
System.out.println("\n\n");
}
private static List getTopPairs(Map contributorsFileMap) {
Map pairFrequency = new HashMap();
for(Set contributors : contributorsFileMap.values()) {
List contributorList = new ArrayList(contributors);
for(int i = 0; i < contributorList.size(); i++) {
for(int j = i + 1; j < contributorList.size(); j++) {
String pair = contributorList.get(i) + " - " + contributorList.get(j);
pairFrequency.put(pair, pairFrequency.getOrDefault(pair, 0) + 1);
}
}
}
return pairFrequency.entrySet().stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.collect(Collectors.toList());
}
private static void displayResult(List sortedPairs) {
if(sortedPairs.isEmpty()) {
System.out.println("\n\nNo contributors pairs found");
return;
}
System.out.println("\n\nTop contributors pairs:");
sortedPairs.stream().limit(10).forEach(pair -> {
System.out.println(pair.getKey() + " : " + pair.getValue());
});
}
private static void parseCommit(GHCommit commit, AtomicInteger counter, int numOfCommits, Map contributorsFileMap) {
int count = counter.incrementAndGet();
int percentage = (int) (((double) count / numOfCommits) * 100);
getProgressBar(percentage);
try {
Optional.ofNullable(commit.getAuthor())
.map(GHPerson::getLogin)
.ifPresent(author->{
try {
commit.getFiles().stream().map(GHCommit.File::getFileName).forEach(filename -> {
contributorsFileMap.computeIfAbsent(filename, k -> new HashSet()).add(author);
});
} catch (IOException e) {
throw new RuntimeException(e);
}
});
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static void getProgressBar(int percentage) {
StringBuilder progressBar = new StringBuilder("[");
for(int i = 0; i < 50; i++) {
if(i < percentage / 2) {
progressBar.append("=");
} else {
progressBar.append(" ");
}
}
progressBar.append("] " + percentage + "%");
System.out.print("\r" + progressBar.toString());
}
}
Подробнее здесь:
https://stackoverflow.com/questions/784 ... tfoundexce
1715201429
Anonymous
Я без проблем запускаю проект в IntelijIdea. Когда я пытаюсь запустить такие команды: [list] [*]Установите свой токен личного доступа GitHub как переменную среды с именем GITHUB_TOKEN. [*]Соберите приложение с помощью команды mvn clean install. [*]После успешной сборки будет создан файл github-analyzer.jar. создается в целевом каталоге. [*]Перейдите в целевой каталог с помощью команды cd target. [*]Запустите приложение с помощью команды java -jar github-analyzer-1.0-SNAPSHOT.jar.` [/list] maven запускается, но в при последнем вызове я получаю эту ошибку: Ошибка: невозможно инициализировать основной класс GitHubAnalyzer Причина: java.lang.NoClassDefFoundError: org/kohsuke/github/GHFileNotFoundException Это мой код, я делаю что-то не так?[code]import org.kohsuke.github.*; import io.github.cdimascio.dotenv.Dotenv; import java.io.IOException; import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; public class GitHubAnalyzer { public static String GITHUB_TOKEN; static { Dotenv dotenv = Dotenv.load(); GITHUB_TOKEN = dotenv.get("GITHUB_TOKEN"); } public static final long WEEK_IN_MILLIS = 7L * 24 * 3600 * 1000; public static void main(String[] args) throws IOException { if(GITHUB_TOKEN.isEmpty()) { System.err.println("Please set the GITHUB_TOKEN environment variable"); return; } try (Scanner scanner = new Scanner(System.in)) { System.out.println("GitHub Analyzer"); System.out.println("Insert owner:"); String owner = scanner.next(); System.out.println("Insert repository:"); String repository = scanner.next(); System.out.println("What do you want to analyze?"); System.out.println("1. Pair of contributors that worked on the same file"); System.out.println("2. Contributions of contributors in the last week"); int choice = scanner.nextInt(); GitHub github; try { github = new GitHubBuilder().withOAuthToken(GITHUB_TOKEN).build(); }catch (IOException e){ System.err.println("An error occurred while accessing GitHub: " + e.getMessage()); return; } analyzeRepository(github, owner, repository, choice); } catch (IOException e) { System.err.println("An error occurred while analyzing the repository: " + e.getMessage()); } } private static void analyzeRepository(GitHub github, String owner, String repository, int choice) throws IOException { GHRepository repo; try { repo = github.getRepository(owner + "/" + repository); } catch (GHFileNotFoundException e) { System.out.println("Repository not found"); return; }catch (IOException e){ System.out.println("An error occurred while accessing the repository: " + e.getMessage()); return; } List commits = new ArrayList(); try { commits = repo.listCommits().withPageSize(100).toList(); }catch (IOException e){ System.out.println("An error occurred while accessing the repository: " + e.getMessage()); return; } int numOfCommits = commits.size(); Map commitsByAuthor = commits.stream() .filter(commit -> { try { return commit.getAuthor() != null; } catch (IOException e) { throw new RuntimeException(e); } }).collect(Collectors.groupingBy(commit -> { try { return commit.getAuthor().getLogin(); } catch (IOException e) { throw new RuntimeException(e); } })); List contributors = repo.listContributors().toList(); Date weekAgo = new Date(System.currentTimeMillis() - WEEK_IN_MILLIS); AtomicInteger counter = new AtomicInteger(0); Map contributorsFileMap = new ConcurrentHashMap(); if (choice == 1) { analyzeContributorsPairs(repo, commits, numOfCommits, counter, contributorsFileMap, contributors); } else if(choice == 2){ analyzeContributorsContributions(repo, commitsByAuthor, weekAgo, counter, contributors); } } private static void analyzeContributorsContributions(GHRepository repo, Map commitsByAuthor, Date weekAgo, AtomicInteger counter, List contributors) throws IOException { System.out.println("Analyzing contributors contributions"); for(GHRepository.Contributor contributor:contributors){ List commitsByThisAuthor = commitsByAuthor.getOrDefault(contributor.getLogin(),Collections.emptyList()); analyzeContributor(repo, contributor, weekAgo,commitsByThisAuthor); } } private static void analyzeContributorsPairs(GHRepository repo, List commits, int numOfCommits, AtomicInteger counter, Map contributorsFileMap, List contributors) { System.out.println("Analyzing contributors pairs"); commits.forEach((commit)-> parseCommit(commit, counter, numOfCommits, contributorsFileMap)); displayResult(getTopPairs(contributorsFileMap)); } private static void analyzeContributor(GHRepository repo, GHRepository.Contributor contributor, Date weekAgo, List commitsByThisAuthor) throws IOException { System.out.println("Analyzing contributor: " + contributor.getLogin()); int linesOfCode = 0; int commitsNo; int mergeRequests; int maxCharsInComment = 0; List commits = commitsByThisAuthor.stream() .filter(commit -> { try { return commit.getCommitDate().after(weekAgo); } catch (IOException e) { throw new RuntimeException(e); } }) .collect(Collectors.toList()); commitsNo = commits.size(); for(GHCommit commit : commits) { for(GHCommit.File file : commit.getFiles()) { linesOfCode += file.getLinesAdded(); } } List pullRequests = repo.getPullRequests(GHIssueState.ALL) .stream().filter(ghPullRequest -> { try { return (ghPullRequest.getUser().getLogin().equals(contributor.getLogin()) && ghPullRequest.getCreatedAt().after(weekAgo)); } catch (IOException e) { throw new RuntimeException(e); } }).collect(Collectors.toList()); mergeRequests = pullRequests.size(); List comments = new ArrayList(); pullRequests.stream().map(ghPullRequest -> { try { return ghPullRequest.listReviewComments(); } catch (IOException e) { throw new RuntimeException(e); } }).forEach(ghPullRequestReviewComments -> { try { comments.addAll(ghPullRequestReviewComments.toList()); } catch (IOException e) { throw new RuntimeException(e); } }); maxCharsInComment = comments.stream(). map(GHPullRequestReviewComment::getBody). map(String::length).max(Integer::compareTo).orElse(0); System.out.println("Contributor: " + contributor.getLogin()); System.out.println("Lines of code: " + linesOfCode); System.out.println("Commits: " + commitsNo); System.out.println("Merge requests: " + mergeRequests); System.out.println("Max chars in comment: " + maxCharsInComment); System.out.println("\n\n"); } private static List getTopPairs(Map contributorsFileMap) { Map pairFrequency = new HashMap(); for(Set contributors : contributorsFileMap.values()) { List contributorList = new ArrayList(contributors); for(int i = 0; i < contributorList.size(); i++) { for(int j = i + 1; j < contributorList.size(); j++) { String pair = contributorList.get(i) + " - " + contributorList.get(j); pairFrequency.put(pair, pairFrequency.getOrDefault(pair, 0) + 1); } } } return pairFrequency.entrySet().stream() .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) .collect(Collectors.toList()); } private static void displayResult(List sortedPairs) { if(sortedPairs.isEmpty()) { System.out.println("\n\nNo contributors pairs found"); return; } System.out.println("\n\nTop contributors pairs:"); sortedPairs.stream().limit(10).forEach(pair -> { System.out.println(pair.getKey() + " : " + pair.getValue()); }); } private static void parseCommit(GHCommit commit, AtomicInteger counter, int numOfCommits, Map contributorsFileMap) { int count = counter.incrementAndGet(); int percentage = (int) (((double) count / numOfCommits) * 100); getProgressBar(percentage); try { Optional.ofNullable(commit.getAuthor()) .map(GHPerson::getLogin) .ifPresent(author->{ try { commit.getFiles().stream().map(GHCommit.File::getFileName).forEach(filename -> { contributorsFileMap.computeIfAbsent(filename, k -> new HashSet()).add(author); }); } catch (IOException e) { throw new RuntimeException(e); } }); } catch (IOException e) { throw new RuntimeException(e); } } private static void getProgressBar(int percentage) { StringBuilder progressBar = new StringBuilder("["); for(int i = 0; i < 50; i++) { if(i < percentage / 2) { progressBar.append("="); } else { progressBar.append(" "); } } progressBar.append("] " + percentage + "%"); System.out.print("\r" + progressBar.toString()); } } [/code] Подробнее здесь: [url]https://stackoverflow.com/questions/78410687/caused-by-java-lang-noclassdeffounderror-org-kohsuke-github-ghfilenotfoundexce[/url]