Ayan Biswas
Ayan Biswas

Reputation: 1635

Gradle-Scala Project Error - Could not load Main Class

I have created a gradle/scala project in intellij. This is my project structure

build
gradle
      - wrapper
META-INF
out
src
   - main
          -resources
          -scala
   - test
         -resources
         -scala
build.gradle
gradlew
gradlew.bat
settings.gradle

These are the contents of my build.gradle file

plugins {
    id 'scala'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
    mavenLocal()
    maven {
        url = uri('<artifactory location>')
    }
}
jar {
    manifest {
        attributes(
                'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
                'Main-Class': 'org.example.Hello'
        )
    }
}
sourceCompatibility = '12'
dependencies {
    implementation 'org.scala-lang:scala-library:2.12.14'
    testImplementation 'org.scalatest:scalatest_2.12:3.0.5'
    testRuntimeOnly 'org.scala-lang.modules:scala-xml_2.12:1.1.1'
}
test {
    useJUnitPlatform()
}

Main Class Content

package org.example
object Hello extends App {
   println("hello!")
}

When I try to build the jar and execute it , it gives an error Could not find or load main class org.example.Hello . What might be the mistake here?

Upvotes: 0

Views: 208

Answers (2)

Ayan Biswas
Ayan Biswas

Reputation: 1635

I was able to resolve the issue by adding the following in the settings.gradle

rootProject.name = 'gradle-scala'
include('gradle-scala')

Basically , the include tag is used to specify the directory where the code resides .

Upvotes: 1

PrasadU
PrasadU

Reputation: 2438

I see out directory, can you check the gradle settings ensure below are ticked

enter image description here

Upvotes: 0

Related Questions