简体   繁体   English

(Gradle)错误:无法找到或加载主 class 主

[英](Gradle) Error: Could not find or load main class Main

I have a java class named Main.java located in app\build\classes\java\main directory and I want to execute Java class by Gradle Task. I have a java class named Main.java located in app\build\classes\java\main directory and I want to execute Java class by Gradle Task. This is my build.gradle file:这是我的build.gradle文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.1"
    defaultConfig {
        applicationId "..."
        minSdkVersion 15
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}


task taskA(type: JavaExec){
        classpath += files("${getBuildDir()}\\classes\\java\\main")
        main = "Main"
}

But when i execute this task, Windows Command Line shows me this error:但是当我执行这个任务时,Windows 命令行向我显示了这个错误:

> Configure project :app
NDK is missing a "platforms" directory.
If you are using NDK, verify the ndk.dir is set to a valid NDK directory.  It is currently set to D:\Dev\Sdk\sdk-AndroidStudio\ndk-bundle.
If you are not using NDK, unset the NDK variable from ANDROID_NDK_HOME or local.properties to remove this warning.


> Task :app:taskA FAILED
Error: Could not find or load main class Main

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:taskA'.
> Process 'command 'C:\Program Files\Java\jdk1.8.0_131\bin\java.exe'' 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 4s
1 actionable task: 1 executed

Main.java主.java

public static class Main  {

    public static void main(String[] args){
        System.out.println("salam hadi joon");
    }
}

What can I do to solve this problem?我能做些什么来解决这个问题? Thanks谢谢

*****text just to be remove mostly code error****** *****文本只是要删除主要是代码错误******

public class Main  {

    public static void main(String[] args){
        System.out.println("salam hadi joon");
    }
}

Change your method from static to non-static .将您的方法从static 更改为 non-static Try this.尝试这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM