【问题标题】:Could not find or load main class in Groovy gradle project无法在 Groovy gradle 项目中找到或加载主类
【发布时间】:2018-09-27 08:55:09
【问题描述】:

我有一个 groovy 应用程序,我想用 gradle 将其打包到可执行 jar 中。 问题是当 jar 完成时出现错误:无法找到或加载主类。 这是我的 build.gradle:

group 'com.demo'
version '1.0-SNAPSHOT'

 apply plugin: 'groovy'
 apply plugin: 'java'
 apply plugin: 'idea'
 apply plugin:'application'
 mainClassName = 'com.demo.App'


 buildscript {
      repositories {
          mavenCentral()
      }
      dependencies {}
 }

  repositories {
   mavenCentral()
  }
  // java version dependency
   sourceCompatibility = 1.8
   targetCompatibility = 1.8

  jar {
baseName = 'cim-configurator'
version =  '0.1.0'
manifest {
    attributes("Build-Time": new Date().format("yyyy-MM-dd HH:mm:ss"),
            "Build-Jdk": System.getProperty("java.version"),
            "Built-By": System.getProperty("user.name"),
            "Created-By": "Gradle",
            "Main-Class": "com.demo.App"
    )
  }
}

这是文件层次结构:

com.demo
   ActiveMq
   App
   Database
   Rbac
   Run.groovy
   Service

【问题讨论】:

  • 你是怎么得到这个错误的?来自哪个命令?
  • 当我尝试使用 java -jar 启动 jar 时
  • 请同时添加文件层次结构。
  • @cfrick 我编辑了问题
  • 我的猜测是,您缺少对groovy 的依赖(例如groovy-all),这里的错误实际上隐藏了它看不到GObject 和朋友的事实。我还不太相信这里的文件是 groovy 的,什么是 java 的,以及实际的源代码是什么(来自你的部分树)。所以我很惊讶这个甚至建成了。

标签: java gradle groovy


【解决方案1】:

我认为使用 uberjar 可以解决您的问题。它适用于我的类似案例。

【讨论】:

    【解决方案2】:

    我在 Groovy & Gradle 项目中有这个 exact same problem。这是the answer that fixed this problem

    TL;DR

    通过将以下插件添加到 build.gradle 中的插件块来使用 Shadow-jar plugin

    plugins {
        id 'com.github.johnrengelman.shadow' version '5.0.0'
    }
    

    然后运行./gradlew shadowJar

    你会得到一个jar文件emailer-all.jar,可以运行。

    【讨论】:

      猜你喜欢
      • 2014-09-15
      • 2018-09-12
      • 1970-01-01
      • 2021-09-18
      • 1970-01-01
      • 1970-01-01
      • 2016-01-21
      • 2018-12-02
      • 1970-01-01
      相关资源
      最近更新 更多