“无法找到或加载主类";Gradle生成的Scala JAR错误 [英] "Could not find or load main class" error for Gradle-generated Scala JAR

查看:83
本文介绍了“无法找到或加载主类";Gradle生成的Scala JAR错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过大量研究,我似乎无法弄清使用Gradle生成可运行的Scala jar文件时遇到的问题.我重写了"jar" Gradle任务,以创建一个从我的主类文件开始执行的jar文件(包括依赖项).但是,无论何时运行它,无论我将Main-Class属性用作什么,控制台都会引发找不到或加载主类"错误.这是我到目前为止的内容:

After much research, I can't seem to get to the root of a problem I am having in generating a runnable Scala jar file using Gradle. I'm overriding the 'jar' Gradle task to create a jar file (dependencies included) that executes starting from my main class file. However, whenever I run it, regardless of what I use for a Main-Class attribute, the console throws a "Could not find or load main class" error. Here's what I have so far:

build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
}

apply plugin: 'java'
apply plugin: 'scala'
apply plugin: 'application'

repositories {
    mavenCentral()
    // some other repos
}

version = '1.0'
sourceCompatibility = 1.8
targetCompatibility = 1.8

mainClassName = "com.test.Main"

dependencies {
    // my dependencies
}

jar {
    zip64 = true

    manifest {
        attributes "Main-Class": "$mainClassName"
    }

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

src/main/scala/com/test/Main.scala

package com.test

object Main {
  def main(args: Array[String]): Unit = {
    print("Hello world");
  }
}

实际上,当我运行"java tf test.jar"时,它在jar的根目录中显示了"com/test/Main.class"!我是否缺少一些重要的课堂路径信息或其他信息?我正在使用Gradle 3.5在macOS Sierra上运行Java 1.8.任何帮助表示赞赏.谢谢!

In fact, when I run "java tf test.jar", it shows "com/test/Main.class" in the root of the jar! Am I missing some important class path info or something? I'm running Java 1.8 on macOS Sierra using Gradle 3.5. Any help is appreciated. Thanks!

推荐答案

我遇到了类似的问题,事实证明我的jar文件中的META-INF文件夹包含一些RSA,SF和DSA文件.

I had a similar problem and it turns out that my META-INF folder inside my jar file contains a few RSA, SF, and DSA files.

一旦我排除了它们,它就会起作用!

Once I excluded them, it worked!

这是基于您的jar声明的方法

here is how to based on your jar declaration

jar {
    zip64 = true

    manifest {
        attributes "Main-Class": "$mainClassName"
    }

    from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) 
    }
    exclude ('META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA')
}

这篇关于“无法找到或加载主类";Gradle生成的Scala JAR错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆