简体   繁体   中英

I'm Beginner to Java, I have a error message with .gradle and Soursets with main. How can I solve this problem?

I'm beginner in Java and Android-Studio. When trying to complete the first step, I came across a big problem.

When I typed my first Java code in Android Studio and I ran the code I received this error message...

FAILURE: Build failed with an exception.

  • Where: Initialization script 'C:\\Users\\forma\\AppData\\Local\\Temp\\asdb_main__.gradle' line: 20

  • What went wrong: A problem occurred configuring project ':app'.

    Could not create task ':app:asdb.main()'.

    > SourceSet with name 'main' not found.

So, I looked for the .gradle file, but I don't know what the problem is.

How can I solve this problem?

I ran this code but I got the error message.

package com.example.myapplication;

public class asdb {
    public static void main(String[] args) {
        int a;
        a=10;
        System.out.println(a);
    }
}

and this is .gralde code including line 20.

def gradlePath = ':app'
def runAppTaskName = 'asdb.main()'
def mainClass = 'com.example.myapplication.asdb'
def javaExePath = 'C:/Program Files/Android/Android Studio/jre/bin/java.exe'
def _workingDir = 'C:/Users/forma/Desktop/practice/java2'

def sourceSetName = 'main'
def javaModuleName = null



allprojects {
  afterEvaluate { project ->
    if(project.path == gradlePath && project?.convention?.findPlugin(JavaPluginConvention)) {
      project.tasks.create(name: runAppTaskName, overwrite: true, type: JavaExec) {
        if (javaExePath) executable = javaExePath
        classpath = project.sourceSets[sourceSetName].runtimeClasspath
        main = mainClass

I am assuming you mean the build.gradle in this answer

have a build.gradle in the root as follows:

plugins {
    id 'java'
}

group 'x'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

and a 'settings.gradle' defining your root project name

essentially let your IDE do all the initalising of the gradle files

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