Gradle,错误:无法找到或加载主类“test.Main”。

9
我在一个项目中实现了Gradle。我使用带有Gradle插件的Netbeans 8.02。
结构应该是正确的,源代码位于jgli/src/main/java/下,资源位于jgli/src/main/resources/下。
主类是jgli/src/main/java/test/Main.java 如果我通过IDE运行它,在Windows上运行良好,在Linux上会崩溃。这就是为什么我现在尝试通过控制台运行它: 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中,但没有任何变化..

这里你可以下载jar包。

这是我的MANIFEST.MF

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

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

也尝试过

apply plugin: 'application'

mainClassName = 'test.Main'

没有成功...

我错过了什么?


当然,“manifest”看起来正确,而且“Main.java”也在那里。 - elect
1
然后检查您的Main.java文件中是否有包声明,例如 package test;。如果没有它,类将无法找到。 - Stanislav
是的,无论如何,IDE 在此之前应该已经触发了它。 - elect
刚刚将预览评论添加为答案。 - Stanislav
等一下,对我来说它只在Windows上运行。因此我现在正在尝试通过控制台运行。你是从哪个操作系统上运行的?怎么运行的? - elect
显示剩余3条评论
2个回答

3

我刚刚发现了你的git 仓库,以及你的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'

如果是这种情况,你的依赖项必须在你运行主类时的类路径中。尝试提供-cp选项来指定路径,以便找到你的依赖项。


我应该只给出-cp folderContainingDependencyJars,是吗?因为它还没有起作用。 - elect
由于Javadoc中的说明:“当您使用-jar选项时,指定的JAR文件是所有用户类的源,其他类路径设置将被忽略。”这意味着您不能使用-jar选项运行它。 - Stanislav
classpath 可以组合多个 jar 包和目录,因此它可以像这样:java -cp jgli.jar;/folder/to/deps/* test.Main,而且您不需要在清单中提供 MainClass 属性。 - Stanislav
当然,PS C:\Users\GBarbieri\Documents\NetBeansProjects\jgli\build\libs> java -cp .\jgli.jar; C:\Users\GBarbieri\Downloads\jogamp-all-platforms\jogamp-all-platforms\jar\* test.Main - elect
@elect 看起来你的路径中有空格,例如在 ; 或者 \joga mp-all-platforms\ 之后,在这种情况下,你需要用 " 将它包围起来,像这样:java -cp ".\jgli.jar;C:\Users\GBarbieri\Downloads\joga mp-all-platforms\jogamp-all-platforms\jar\*" test.Main。因为在你的情况下,-cp 值在分号之后结束,而库文件夹的路径被视为一个新参数。 - Stanislav
显示剩余6条评论

2
通常情况下,您需要在构建文件中设置主类,如下所示:

mainClassName = 'package.MainClassName'

你将其作为参数传递吗?-> hasProperty 然后你需要运行类似于以下的构建命令:gradle -PmainClass=MyClass build

mainClassNameapplication插件使用,但不被java使用。如果您不应用此插件,您将会得到“没有这样的属性”异常,因为该属性将无法找到。 - Stanislav

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接