java - 添加依赖项时:“找不到或加载主类”
问题描述
当我添加
compile group: 'com.azure', name: 'azure-cosmos', version: '4.0.1-beta.4'
我的 Jar 文件中的依赖build.gradle
项将不再执行。
我基本上从头开始创建一个新项目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'
)
}
}
推荐阅读
- javascript - AWS Lambda 到 AWS Lambda 权限
- testing - Testcafe 等到一个元素有特定的文本
- python - 格式化python时小数位数被四舍五入
- python - 芹菜弦,没有执行回调函数
- postgresql - 从Postgresql中的列中提取具有多个单词的条目中的单个单词
- sql - 我对这个数据库感到困惑,我应该创建更多外键还是更多列?
- python - Python HTML抓取找不到我知道存在的属性?
- git - 如何使用组织 git hub 登录 Gitkraken
- r - 如何在列中查找所有组合并计算数据中的出现次数
- linux - 如何使用 grep 匹配作为变量(而不是整行)