首页 > 解决方案 > 添加依赖项时:“找不到或加载主类”

问题描述

当我添加

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());
    }
}

标签: javaazuregradleazure-cosmosdb

解决方案


这将起作用:

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'
    )
  }
}

推荐阅读