【问题标题】:Gradle, error could not find or load main class 'test.Main'Gradle,错误无法找到或加载主类'test.Main'
【发布时间】:2016-01-21 09:28:04
【问题描述】:

我在 one 的项目中实现了 Gradle。我使用带有 gradle 插件的 Netbeans 8.02。

结构应该是这样,来源位于jgli/src/main/java/下,资源位于jgli/src/main/resources/

主类是jgli/src/main/java/test/Main.java

如果我通过 ide 运行它,它会在 windows 上运行,它在 linux 上运行 crashes。这就是我现在尝试通过控制台运行它的原因:

java -jar jgli.jar

但我不断得到:

错误无法找到或加载主类'test.Main'

这是我的build.gradle

apply plugin: 'java'

sourceCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

// NetBeans will automatically add "run" and "debug" tasks relying on the
// "mainClass" property. You may however define the property prior executing
// tasks by passing a "-PmainClass=<QUALIFIED_CLASS_NAME>" argument.
//
// Note however, that you may define your own "run" and "debug" task if you
// prefer. In this case NetBeans will not add these tasks but you may rely on
// your own implementation.
if (!hasProperty('mainClass')) {
    ext.mainClass = ''
}

repositories {
    mavenCentral()
    // You may define additional repositories, or even remove "mavenCentral()".
    // Read more about repositories here:
    //   http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:repositories
}

dependencies {
    // TODO: Add dependencies here ...
    // You can read more about how to add dependency here:
    //   http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:how_to_declare_your_dependencies
    testCompile group: 'junit', name: 'junit', version: '4.10'

    compile 'org.jogamp.jogl:jogl-all-main:2.3.2'
    compile 'org.jogamp.jogl:nativewindow-main:2.3.2'
    compile 'org.jogamp.jogl:newt-main:2.3.2'
    compile 'org.jogamp.gluegen:gluegen-rt-main:2.3.2'
}

jar {
    manifest {
        attributes 'Main-Class': 'test.Main'
    }
}

我刚刚修改了dependecies 部分并添加了清单部分。

我尝试将'test.Main' 添加到 ext.mainClass,但没有任何改变..

Here你可以下载jar。

这是我的MANIFEST.MF

Manifest-Version: 1.0
Main-Class: test.Main

Main.class 正确地位于 jar 中。 Main.java 有包声明package test;

也试过了

apply plugin: 'application'

mainClassName = 'test.Main'

没有成功..

我错过了什么?

【问题讨论】:

  • 您能否检查一下,您的 jar 是否包含具有 MainClass 属性的有效清单文件,并且此 jar 中提供了您的 Main 类?
  • 当然,manifest 似乎是正确的,Main.java 在那里
  • 然后检查您的 Main.java 文件中是否包含包声明,例如 package test;。没有它,类就找不到了
  • 是的,它有,反正 ide 之前会触发它。
  • 我刚刚找到了你的 git repo 和 Main 类的来源,似乎它实现了一些接口,这些接口在你的依赖项中提供。在这种情况下,当您运行 Main 类时,您的依赖项必须位于您的类路径中。尝试提供-cp 选项来指向依赖jar的路径

标签: java gradle find main netbeans-8


【解决方案1】:

通常您在构建文件中设置主类如下:

mainClassName = 'package.MainClassName'

您是否将其作为参数传递? -> 拥有属性 然后你必须像这样运行你的构建: gradle -PmainClass=MyClass build

【讨论】:

  • mainClassNameapplication 插件使用,但不是java。如果你不应用这个插件,你会得到 No such property 异常,因为这个属性不会被找到。
【解决方案2】:

我刚刚找到你的 git repository 和你的 Main class 的来源,似乎它实现了一些接口,这些接口在你的依赖项中提供:

import com.jogamp.newt.event.KeyListener;
import com.jogamp.opengl.util.GLEventListener;

public class Main implements GLEventListener, KeyListener {
  ...

哪些来自您的一些依赖库:

compile 'org.jogamp.jogl:jogl-all-main:2.3.2'
compile 'org.jogamp.jogl:nativewindow-main:2.3.2'
compile 'org.jogamp.jogl:newt-main:2.3.2'
compile 'org.jogamp.gluegen:gluegen-rt-main:2.3.2'

在这种情况下,当您运行 Main 类时,您的依赖项必须位于类路径中。尝试提供-cp 选项来指向可以找到依赖项的路径。

【讨论】:

  • 我应该只给-cp folderContainingDependencyJars,不是吗?因为还不行
  • @elect due to javadoc "当你使用 -jar 选项时,指定的 JAR 文件是所有用户类的来源,其他类路径设置被忽略。"这意味着,您不能使用 -jar 选项运行它
  • 嗯,好的,那我该怎么办?将我所有的依赖项复制到我的 jar 的同一个文件夹中?
  • classpath 可以结合 jars 和目录的数量,所以它可能类似于 java -cp jgli.jar;/folder/to/deps/* test.Main 并且不需要在清单中提供 MainClass 属性
  • @elect 您能否提供您尝试运行的命令?
猜你喜欢
  • 2021-09-18
  • 2014-09-15
  • 1970-01-01
  • 2018-09-12
  • 2016-06-12
  • 2016-03-16
  • 2016-01-03
  • 1970-01-01
相关资源
最近更新 更多