Packaging spring-boot app w/deps can't find main class when running from java -jar


#1

I’m building my first kotlin spring-boot app w/jpa and a few controllers. It works fine when running within Intellij ultimate, but I need a working self-contained jar. I’m hitting a roadblock here when trying to run the self-contained jar.

First, I added the plugin in my pom.xml exactly as instructed in the url below.
instructions I’m following: https://kotlinlang.org/docs/reference/using-maven.html

After running mvn install or mvn package. My resulting target/my-api-0.0.1-SNAPSHOT-jar-with-dependencies.jar is 68M

Within the .jar class files exist as expected
BOOT-INF/classes/com/myproject/

  • MyApplication$init$1.class
  • MyApplication.class
  • MyApplicationKt.class

And in META-INF/MANIFEST.mf the main class matches what I set for main.class in my pom.xml

Here’s the error I get when running the jar

java -jar target/my-api-0.0.1-SNAPSHOT-jar-with-dependencies.jar

Error: Could not find or load main class com.myproject.MyApplicationKt (or without Kt depending on how I set main.class in my pom)

Heres what I’ve tried so far to get this self-contained jar working:

  1. in pom.xml tried both
    <main.class>com.myproject.MyApplicationKt</main.class> &&<main.class>com.myproject.MyApplication</main.class>

    package com.myproject
    
    @SpringBootApplication
    class MyApplication : WebMvcConfigurerAdapter() { 
    // snip ...
       }
    
    fun main(args: Array<String>) {
        SpringApplication.run(MyApplication::class.java, *args)
    }
    
  2. in pom.xml (same as before) tried both
    <main.class>com.myproject.MyApplicationKt</main.class> &&
    <main.class>com.myproject.MyApplication</main.class>

but instead w/ a companion object inside MyApplication. i.e.

class MyApplication : WebMvcConfigurerAdapter() { 
   companion object {
@JvmStatic fun main(args: Array<String>) {
    SpringApplication.run(Application::class.java, *args)
       }
}

}

Lastly:

  • kotlin: 1.1.4
  • spring boot: 1.5.6.RELEASE
  • maven: 3.3.9

#2

If you extract files from your jar file, it should contain /com/myproject/MyApplicationKt.class file. I’m not sure what is BOOT-INF/classes prefix, it’s certainly not a correct jar-file. If you mean WEB-INF/classes, it means that you built a war file, not jar file. war-files are meant to be deployed into application servers, like Tomcat, they are not runnable jar files.


#3

Alright cool, so we’re onto something then.

So maybe it’s the spring-boot plugin moving the class files into this folder. Does anyone know how to get the sprint-boot maven plugin and kotlin maven plugins to harmonize in this scenario?