C.D.
C.D.

Reputation: 466

Gradle builds successfully without SourceSet, but fails with SourceSet, Error Cannot Find Symbol

I have a stupidly simple HelloWorld program that works if I do not use Gradle SourceSet, but fails if I do. I cannot understand why. Please help.

Using Gradle 6.6.1-bin. I installed it manually myself, just to make sure it was compatible.

The problem is, if I place the build.gradle file and the HelloWorld.java file in the same base directory, everything is compiles and builds successfully. Example commands:

gradle clean build

HelloWorld.java:

package hello;

import org.joda.time.LocalTime;

public class HelloWorld {
  public static void main(String[] args) {
    LocalTime currentTime = new LocalTime();
    System.out.println("The current local time is: " + currentTime);

    Greeter greeter = new Greeter();
    System.out.println(greeter.sayHello());
  }
}

build.gradle without SourceSet:

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'application'

mainClassName = 'hello.HelloWorld'

repositories {
    mavenCentral()
}

jar {
    archiveBaseName = 'gs-gradle'
    archiveVersion =  '0.1.0'
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    implementation "joda-time:joda-time:2.2"
    testImplementation "junit:junit:4.12"
}

HOWEVER if I try to move the source files to a folder layout (as the Gradle guides suggest is their standard) and add a SourceSet, it fails.

The folder layout would be this:

Modified build.gradle with SourceSet:

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'application'


sourceSets {
    main {
        java {
            srcDir 'src'
        }
    }
}

mainClassName = 'hello.HelloWorld'

repositories {
    mavenCentral()
}

jar {
    archiveBaseName = 'gs-gradle'
    archiveVersion =  '0.1.0'
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    implementation "joda-time:joda-time:2.2"
    testImplementation "junit:junit:4.12"
}

Now running this fails:

gradle clean build

The console output:

> Task :compileJava FAILED
/opt/myProject/src/main/java/HelloWorld.java:10: error: cannot find symbol
    Greeter greeter = new Greeter();
    ^
  symbol:   class Greeter
  location: class HelloWorld
/opt/myProject/src/main/java/HelloWorld.java:10: error: cannot find symbol
    Greeter greeter = new Greeter();
                          ^
  symbol:   class Greeter
  location: class HelloWorld
2 errors

FAILURE: Build failed with an exception.

I do not know why this is failing to work.

Upvotes: 0

Views: 2181

Answers (2)

I think the problem is caused by the SDK version not being compatible. Just go to build.gradle then use JDK 21 instead of 17 (or whatever version was already there):

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(21)
    }
}

Upvotes: 0

C.D.
C.D.

Reputation: 466

As @M.Ricciuti pointed out, I simply needed to remove the sourceSets block from the second build.gradle script. By default Gradle will use src/main/java as base directory for main sourceSet, without needing it to be explicitly declared. (I was unaware of this.)

Upvotes: 0

Related Questions