I'm trying to run a jar file built from a kotlin s...
# gradle
j
I'm trying to run a jar file built from a kotlin spring boot project, but I'm getting a
java.lang.ClassNotFoundException
. I believe the issue has to do with my build.gradle.kts file, which I'll put in a thread below.
Copy code
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
	id("org.springframework.boot") version "2.5.3"
	id("io.spring.dependency-management") version "1.0.11.RELEASE"
	kotlin("jvm") version "1.5.21"
	kotlin("plugin.spring") version "1.5.21"
}

group = "com.example"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_15

configurations {
	compileOnly {
		extendsFrom(configurations.annotationProcessor.get())
	}
}

repositories {
	mavenCentral()
}

dependencies {
	implementation("org.springframework.boot:spring-boot-starter-data-mongodb")
	implementation("org.springframework.boot:spring-boot-starter-web")
	implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
	implementation("org.jetbrains.kotlin:kotlin-reflect")
	implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
	compileOnly("org.projectlombok:lombok")
	annotationProcessor("org.projectlombok:lombok")
	testImplementation("org.springframework.boot:spring-boot-starter-test")

    implementation("com.graphql-java-kickstart:graphql-spring-boot-starter:11.1.0")
    implementation("com.graphql-java-kickstart:graphiql-spring-boot-starter:11.1.0")
	implementation("com.graphql-java-kickstart:graphql-java-tools:11.0.1")
}

tasks.withType<KotlinCompile> {
	kotlinOptions {
		freeCompilerArgs = listOf("-Xjsr305=strict")
		jvmTarget = "15"
	}
}

tasks.jar {
	manifest {
		attributes["Main-Class"] = "com.example.kotlingraphql.KotlinGraphqlApplicationKt"
	}
}

tasks.withType<Test> {
	useJUnitPlatform()
}

springBoot {
	mainClass.set("com.example.kotlingraphql.KotlinGraphqlApplicationKt")
}
the main kotlin file is as follows:
Copy code
package com.example.kotlingraphql

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories


@SpringBootApplication
@EnableMongoRepositories(basePackages = ["com.example.kotlingraphql.repository"])
class KotlinGraphqlApplication

fun main(args: Array<String>) {
	runApplication<KotlinGraphqlApplication>(*args)
}
I think a potential solution is the first answer in this stackoverflow post, but i'm unsure how to translate the gradle file to a kotlin script
v
No, that SO question is a totally different case and problem. There are two things to note in your build script. 1. you shouldn't use spring dependency management, it is from a time Gradle did not have built-in BOM support, just use
implementation(platform("the spring boot bom"))
instead. 2. Adding a main class manifest attribute manually is in the best case just unnecessary, in the worst case it is causing your issue.
k
do you run bootJar task?
v
The spring boot plugin disables the normal
jar
task, doesn't it? So I just assumed he does. But well, then modifying the standard
jar
task also would not disturb but just be useless. Can you maybe provide an MCVE that reproduces the problem?
k
I can run jar task in my project
as far as I know it will create jar without dependencies
which would explain class not found exceptions
👍 1
j
Interesting, I had to add the main class manifest attribute because the /build/libs/ directory did not show up until I added it (i'm hoping to deploy this project onto heroku, and heroku looks for the jar file there)
Before reading the replies, I had managed to find the translation and added this:
Copy code
from(sourceSets.main.get().output)
	dependsOn(configurations.runtimeClasspath)
	from({
		configurations.runtimeClasspath.get().map { if (it.isDirectory) it else zipTree(it)}
	})
to the tasks.jar block, and after some builds, the error went away (although build is now failing on heroku and i suppose that's an issue that cannot be solved here)
v
You are now mixing two approaches to build a fat jar, that's why I said that this SO question does not apply to your situation. The good one (using spring boot) and an imho bad-practice rape-of-java-features way (manually repacking all libraries into your jar) which can very easily be done wrongly and sometimes even cannot work. For more quotes see https://fatjar.net. You should either focus on getting heroku to work with the jar built by spring boot, or build a proper application if Heroku can handle that. Actually in https://devcenter.heroku.com/articles/deploying-gradle-apps-on-heroku#verify-that-your-build-file-is-set-up-correctly the Heroku help even states that it explicitly supports spring boot. Actually the same page also describes how you would get your dependencies considered without building a fat jar and without using spring boot.