简体   繁体   中英

(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. This is my build.gradle file:

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:

> 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

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 . Try this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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