How to Run A main Method from a Gradle Dependency

I’m trying to run a Main method from a dependency. Here is the build.gradle file:

  plugins {
      id 'java'
      id "com.katalon.gradle-plugin" version "0.0.7"
    }

repositories {
  mavenCentral()
  
  jcenter()
  
  maven {
        url = 'http://companyJARRepository/nexus/content/repositories/releases'
    }
}

dependencies {
    implementation("com.company:DependencyJar:version"){
            exclude group: "org.codehaus.groovy"
        }
}


/**
 * Run Program
 */
task runDependencyProgram(type: JavaExec, dependsOn: configurations.compileClasspath) {
    main = "com.company.MainProgram"
    classpath = sourceSets.main.runtimeClasspath

}

My IDE (IntelliJ in my case) is able to see the dependency, and I know that the MainProgram class is defined in it. I am getting a message saying “Error: Could not find or load main class com.company.MainProgram” so it seems like even though my project can see the dependency, this task cannot, and I’m just not sure why. How do I make it so that my task sources from the dependency?