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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33275309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:27:35  来源:igfitidea点击:

Gradle, error could not find or load main class 'test.Main'

javagradlefindmainnetbeans-8

提问by elect

I implemented Gradle on oneof my projects. I use Netbeans 8.02 with the gradle plugin.

我在我的一个项目中实现了 Gradle 。我将 Netbeans 8.02 与 gradle 插件一起使用。

Structure is as it should be, sources are located under jgli/src/main/java/, resources under jgli/src/main/resources/

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

The Main class is jgli/src/main/java/test/Main.java

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

If I run it through the ide, it runs on windows, it crasheson linux. That's why I am trying to run it through the console right now:

如果我通过 ide 运行它,它会在 Windows上运行,它会在 linux上崩溃。这就是为什么我现在试图通过控制台运行它:

java -jar jgli.jar

java -jar jgli.jar

But I keep getting:

但我不断得到:

Error could not find or load main class 'test.Main'

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

This is my build.gradle

这是我的 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'
    }
}

I just modified the dependeciessection and added the manifest part.

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

I tried to add 'test.Main'to the ext.mainClass, but it changed nothing..

我试图添加'test.Main'到 ext.mainClass,但它什么也没改变。

Hereyou can download the jar.

在这里你可以下载jar。

This is my MANIFEST.MF:

这是我的MANIFEST.MF

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

Main.class is properly located inside the jar. Main.java has the package declaration package test;

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

Tried also

也试过

apply plugin: 'application'

mainClassName = 'test.Main'

without success..

没有成功..

What am I missing?

我错过了什么?

采纳答案by Stanislav

I've just found your git repositoryand sources of your Main class, seems that it implements some interface, which is provided in your dependencies:

我刚刚找到了您的 git存储库Main 类的来源,它似乎实现了一些接口,该接口在您的依赖项中提供:

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

public class Main implements GLEventListener, KeyListener {
  ...

Which are from some of your dependencies libs:

它们来自您的一些依赖库:

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'

In that case, your dependencies must be in your class path, while you running your Main class. Try to provide -cpoption to point the path, where your dependecies could be found.

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

回答by Beat Jost

Normally you set the main-class in your build file as follows:

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

mainClassName = 'package.MainClassName'

Do you pass it as Parameter? -> hasProperty Then you had to run your build something like: gradle -PmainClass=MyClass build

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