Java找不到主类

时间:2019-09-22 15:39:00

标签: java gradle console main

我正在尝试执行小类:

    package com.example.desktop;

    import com.badlogic.gdx.ApplicationListener;
    import com.badlogic.gdx.backends.lwjgl.LwjglApplication;

    public class DesktopLauncher extends LwjglApplication {
        public DesktopLauncher(ApplicationListener listener, String title, int width, int height) {
            super(listener, title, width, height);
        }

        public static void main(String[] args) {
            LwjglApplication application = new DesktopLauncher(null, "lol", 500, 500);
        }
    }

我使用gradle对其进行了编译:

apply plugin: 'java'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation group: 'com.badlogicgames.gdx', name: 'gdx-backend-lwjgl', version: '1.9.9'
}


tasks.whenTaskAdded { runTask ->
    if (runTask.name.contains("myRun")) {
        runTask.dependsOn 'build'
    }
}

task myRun(type: JavaExec) {
    println(sourceSets.main.runtimeClasspath)
    classpath sourceSets.main.runtimeClasspath
    main = "com.example.desktop.DesktopLancher"
}


sourceCompatibility = "8"
targetCompatibility = "8"

但是执行失败:

MacBook-Pro-Piotr:pandemonium-project psuwala$ ./gradlew :desktop:myRun

> Configure project :app
WARNING: Configuration 'testCompile' is obsolete and has been replaced with 'testImplementation'.
It will be removed at the end of 2018. For more information see: http://d.android.com/r/tools/update-dependency-configurations.html
WARNING: Configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api'.
It will be removed at the end of 2018. For more information see: http://d.android.com/r/tools/update-dependency-configurations.html

> Configure project :desktop
file collection

> Task :desktop:myRun FAILED
Error: Could not find or load main class com.example.desktop.DesktopLancher

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':desktop:myRun'.
> Process 'command '/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 0s
3 actionable tasks: 1 executed, 2 up-to-date
MacBook-Pro-Piotr:pandemonium-project psuwala$ 

当我尝试手动执行它时,没有更好的事情发生了。

MacBook-Pro-Piotr:main psuwala$ pwd
/Users/psuwala/projects/pandemonium-project/desktop/build/classes/java/main
MacBook-Pro-Piotr:main psuwala$ javap com/example/desktop/DesktopLauncher
Compiled from "DesktopLauncher.java"
public class com.example.desktop.DesktopLauncher extends com.badlogic.gdx.backends.lwjgl.LwjglApplication {
  public com.example.desktop.DesktopLauncher(com.badlogic.gdx.ApplicationListener, java.lang.String, int, int);
  public static void main(java.lang.String[]);
}
MacBook-Pro-Piotr:main psuwala$ java com/example/desktop/DesktopLauncher
Error: Could not find or load main class com.example.desktop.DesktopLauncher
MacBook-Pro-Piotr:main psuwala$ 

从gradle打印的运行时类路径为:

/Users/psuwala/projects/pandemonium-project/desktop/build/classes/java/main:
/Users/psuwala/projects/pandemonium-project/desktop/build/resources/main:

/Users/psuwala/.gradle/caches/modules-2/files-2.1/com.badlogicgames.gdx/gdx-backend-lwjgl/1.9.9/ca8911cb94c0bfeb59f99e46ca9b92440958fb0e/gdx-backend-lwjgl-1.9.9.jar:
/Users/psuwala/.gradle/caches/modules-2/files-2.1/com.badlogicgames.gdx/gdx/1.9.9/bf26f2f816b3cfe37ac6e66615391c30714b7030/gdx-1.9.9.jar:
/Users/psuwala/.gradle/caches/modules-2/files-2.1/org.lwjgl.lwjgl/lwjgl_util/2.9.2/4b9e37300a87799856e0bd15ed81663cdb6b0947/lwjgl_util-2.9.2.jar:/Users/psuwala/.gradle/caches/modules-2/files-2.1/org.lwjgl.lwjgl/lwjgl/2.9.2/a9d80fe5935c7a9149f6584d9777cfd471f65489/lwjgl-2.9.2.jar:/Users/psuwala/.gradle/caches/modules-2/files-2.1/com.badlogicgames.jlayer/jlayer/1.0.1-gdx/7cca83cec5c1b2f011362f4d85aabd71a73b049d/jlayer-1.0.1-gdx.jar:/Users/psuwala/.gradle/caches/modules-2/files-2.1/org.jcraft/jorbis/0.0.17/8872d22b293e8f5d7d56ff92be966e6dc28ebdc6/jorbis-0.0.17.jar:/Users/psuwala/.gradle/caches/modules-2/files-2.1/org.lwjgl.lwjgl/lwjgl-platform/2.9.2/510c7d317f5e9e700b9cfaac5fd38bdebf0702e0/lwjgl-platform-2.9.2-natives-windows.jar:/Users/psuwala/.gradle/caches/modules-2/files-2.1/org.lwjgl.lwjgl/lwjgl-platform/2.9.2/d276cdf61fe2b516c7b7f4aa1b8dea91dbdc8d56/lwjgl-platform-2.9.2-natives-linux.jar:/Users/psuwala/.gradle/caches/modules-2/files-2.1/org.lwjgl.lwjgl/lwjgl-platform/2.9.2/d55b46b40b40249d627a83a7f7f22649709d70c3/lwjgl-platform-2.9.2-natives-osx.jar:/Users/psuwala/.gradle/caches/modules-2/files-2.1/net.java.jinput/jinput/2.0.5/39c7796b469a600f72380316f6b1f11db6c2c7c4/jinput-2.0.5.jar:/Users/psuwala/.gradle/caches/modules-2/files-2.1/net.java.jutils/jutils/1.0.0/e12fe1fda814bd348c1579329c86943d2cd3c6a6/jutils-1.0.0.jar:/Users/psuwala/.gradle/caches/modules-2/files-2.1/net.java.jinput/jinput-platform/2.0.5/7ff832a6eb9ab6a767f1ade2b548092d0fa64795/jinput-platform-2.0.5-natives-linux.jar:/Users/psuwala/.gradle/caches/modules-2/files-2.1/net.java.jinput/jinput-platform/2.0.5/385ee093e01f587f30ee1c8a2ee7d408fd732e16/jinput-platform-2.0.5-natives-windows.jar:/Users/psuwala/.gradle/caches/modules-2/files-2.1/net.java.jinput/jinput-platform/2.0.5/53f9c919f34d2ca9de8c51fc4e1e8282029a9232/jinput-platform-2.0.5-natives-osx.jar

java版本:

MacBook-Pro-Piotr:main psuwala$ java -version
openjdk version "1.8.0_212"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_212-b04)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.212-b04, mixed mode)

我不知道为什么会这样发生。 知道如何正确执行吗?

1 个答案:

答案 0 :(得分:0)

您可能只需要解决“ Desktop Lan cher”和“ Desktop Laun cher”的拼写错误:-)