0

Ok so first of all, I want to preface this by saying I started working on this project as a vanilla intellij java project and when my build became impossible due to complex dynamic dependencies I converted to gradle using this guide https://www.jetbrains.com/help/idea/gradle.html#:~:text=Convert%20a%20regular%20project%20into%20a%20Gradle%20project%EF%BB%BF,gradle%20and%20click%20OK.

I am very new to gradle and I am struggling a bit. My project structure looks like this

-BigProject
    -SmallProjectA_Main
        -src
            -META-INF
            SmallProjectA_Main.java
        SmallProjectA_Main.iml
    -SmallProjectA
        -src
            SmallProjectA.java
        SmallProjectA.iml
    -SmallProjectB_Main 
        -src
            -META-INF
            SmallProjectB_Main.java
        SmallProjectB_Main.iml
    -SmallProjectB
        -src
            SmallProjectB.java
        SmallProjectB.iml
    -SmallProject_Common
        -src
            (...)
        SmallProject_Common.iml
    build.gradle
    settings.gradle       

My build.gradle looks like this

plugins {
    id 'java'
    id 'application'
}

group 'org.example'
version '1.0'
mainClassName = 'SmallProjectA_Main' //this is what I want to run for now

repositories {
    mavenCentral()
}

sourceSets {
    main {
        java {
            srcDirs = ['src']
        }
    }
}

dependencies {
    implementation files(...bunch of files here...)
}

My settings.gradle file looks like this:

rootProject.name = 'BigProject'

For now, what I want to do is to be able to run SmallProjectA_Main (which has a main method) from command line using gradle run. Later I would like to be able to be able to choose to run either SmallProjectA_Main or SmallProjectB_Main to run, but I need to know if that is advised or if I should just split them into two different projects and have them import common as a dependency.

When I gradle build it says everything is fine, but when I try gradle run it says Could not find or load main class SmallProjectA_Main.

I tried changing the main class name to a lot of things, like SmallProjectA_Main.SmallProjectA_Main or BigProject.SmallProjectA_Main.SmallProjectA_Main or BigProject.SmallProjectA_Main and nothing seems to work.

Would love for some advice, I have been on this error for the better part of the day, tried a lot of stuff from this question: Gradle - Could not find or load main class and none have worked.

  • Do you have one `build.gradle` per subproject? What's the contents of `settings.gradle`? `implementation files(...bunch of files here...)` looks unusual to me, what files are you importing? Have you tried following the docs on structuring multimodule projects? https://docs.gradle.org/current/userguide/structuring_software_products.html – aSemy Oct 03 '22 at 07:07
  • I have just managed to solve this, I'll go over your questions one by one – Tomer Trashman Oct 03 '22 at 09:46
  • I have no build.gradle in any of the subprojects. – Tomer Trashman Oct 03 '22 at 09:46
  • Contents of settings.gradle are written in the questions. – Tomer Trashman Oct 03 '22 at 09:47
  • The contents of implementation files are a bunch of jars whose location is dependant on an enviroment variable – Tomer Trashman Oct 03 '22 at 09:48

1 Answers1

0

I managed to solve my problem. Turns out

sourceSets {
    main {
        java {
            srcDirs = ['src']
        }
    }
}

Is incorrect, as src isnt a recursive src dir. I added each src dir and it worked flawlessly.