首页 文章

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

提问于
浏览
7

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

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

Main类是 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'

没有成功..

我错过了什么?

2 回答

  • 3

    我刚刚发现你的git repositoryMain 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 选项以指向可以找到您的dependecies的路径 .

  • 1

    通常,您在构建文件中设置main-class,如下所示:

    mainClassName = 'package.MainClassName'
    

    你把它作为参数传递? - > hasProperty然后你必须运行你的构建类似:gradle -PmainClass = MyClass build

相关问题