gpt4 book ai didi

java - 添加依赖时: "could not find or load Main Class"

转载 作者:行者123 更新时间:2023-12-01 16:21:30 26 4
gpt4 key购买 nike

当我添加时

compile group: 'com.azure', name: 'azure-cosmos', version: '4.0.1-beta.4'

build.gradle 中我的依赖项我的 Jar 文件将不再执行。

我基本上从头开始创建一个新项目 gradle init并添加此依赖项。

当我运行 java -jar <jarFile> 时,它抛出“错误:无法找到或加载主类”

没有依赖项,它可以正常工作并打印“Hello World”。

我在这里缺少什么?

我当前的build.gradle看起来如下:

/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java project to get you started.
* For more details take a look at the Java Quickstart chapter in the Gradle
* User Manual available at https://docs.gradle.org/6.4.1/userguide/tutorial_java_projects.html
*/

plugins {
// Apply the java plugin to add support for Java
id 'java'

// Apply the application plugin to add support for building a CLI application.
id 'application'
}

repositories {
// Use jcenter for resolving dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}

dependencies {
// This dependency is used by the application.
implementation 'com.google.guava:guava:28.2-jre'
// https://mvnrepository.com/artifact/com.microsoft.azure/azure-cosmos
compile group: 'com.azure', name: 'azure-cosmos', version: '4.0.1-beta.4'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
}

jar {
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}

manifest {
attributes (
'Main-Class': 'gradle.cosmos.example.App'
)
}
}

应用程序类如下所示:

/*
* This Java source file was generated by the Gradle 'init' task.
*/
package gradle.cosmos.example;

public class App {
public String getGreeting() {
return "Hello world.";
}

public static void main(String[] args) {
System.out.println(new App().getGreeting());
}
}

最佳答案

这会起作用:

plugins {  
id 'java'
id 'application'
}

repositories {
jcenter()
}

dependencies {
implementation 'com.google.guava:guava:28.2-jre'
compile group: 'com.azure', name: 'azure-cosmos', version: '4.0.1-beta.4'
testCompile group: 'junit', name: 'junit', version: '4.12'
}

jar {
manifest {
attributes(
'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
'Main-Class': 'gradle.cosmos.example.App'
)
}
}

关于java - 添加依赖时: "could not find or load Main Class",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62260387/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com