简体   繁体   English

添加依赖项时:“无法找到或加载主类”

[英]When adding Dependency: “could not find or load Main Class”

when I add当我添加

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

to my dependencies in the build.gradle my Jar File will not execute anymore.对于我在build.gradle中的依赖项,我的 Jar 文件将不再执行。

I basically create a new project from scratch with gradle init and add this dependency.我基本上使用gradle init从头开始创建一个新项目并添加此依赖项。

It throws an "Error: Could not find or load main class", when I run java -jar <jarFile>当我运行java -jar <jarFile>时,它会引发“错误:找不到或加载主类”

Without the dependency, it works fine and prints the "Hello World."没有依赖,它可以正常工作并打印“Hello World”。

What am I missing here?我在这里想念什么?

My current build.gradle looks as follow:我当前的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'
        )
    }
}

App Class looks as follow:应用程序 Class 如下所示:

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

This will work:这将起作用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM