4

I have problem assembling Kotlin project with some dependencies to JAR file using Maven.

How am i assembling project to JAR:

RightPanel -> MavenProjects -> Lifecycle -> package -> run

When i running JAR file:

java -jar path.jar

I'm getting following error:

no main manifest attribute, in path.jar

I've added maven-assembly-plugin like here:

So my plugins directory looks like this:

<build>
    <sourceDirectory>src/main/kotlin</sourceDirectory>
    <testSourceDirectory>src/test/kotlin</testSourceDirectory>

    <plugins>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals> <goal>single</goal> </goals>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>${main.class}</mainClass>
                            </manifest>
                        </archive>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-plugin</artifactId>
            <version>${kotlin.version}</version>
            <executions>
                <execution>
                    <id>compile</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>test-compile</id>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

main.class property defined here:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <kotlin.version>1.1.51</kotlin.version>
    <junit.version>4.12</junit.version>
    <main.class>ru.icarumbas.main.HelloKt</main.class>
</properties>

Some facts:

  • Hello.kt is starter class and it has fun main(...){}
  • When i unarchive JAR it has META-INF folder.

Versions:

  • Platform: Mac OS
  • Kotlin version: 1.1.51
  • Maven version: 4.0.0

Maybe i'm missing something. I've already looked on a lot of questions alike this, and they didn't help me. So please don't mark this as duplicate and write a comment if you want to see more code. I didn't show my full pom.xml file so write me if you want to see it full.

icarumbas
  • 1,777
  • 3
  • 17
  • 30
  • Hi @icarumbas, have you found a solution on this? – KennethC Aug 04 '20 at 17:43
  • @KennethC yes, i use gradle – icarumbas Aug 05 '20 at 11:35
  • I see.. too bad my library only supports maven.. thanks for the reply! – KennethC Aug 06 '20 at 12:44
  • @KennethC i'm not completely sure, but i think library doesn't know anything about what dependency build system you're using. – icarumbas Aug 07 '20 at 04:55
  • i think for selenium, they do.. https://www.selenium.dev/documentation/en/selenium_installation/installing_selenium_libraries/ "Due to missing native language bindings for Kotlin, you have to use the Java Bindings, e.g. with maven Java" – KennethC Aug 07 '20 at 05:17
  • @KennethC they're talking about java bindings compatible with Kotlin there, not about maven or gradle. I think you were confused by this line. Just try with gradle) – icarumbas Aug 07 '20 at 09:30
  • 1
    Woah. you're right, it worked with gradle. thanks sir!! – KennethC Aug 08 '20 at 09:04

3 Answers3

5

I've got such problems with kotlin and maven. I've solved it in following way (Application.kt):

package org.somepackage

open class Application {
    companion object {
        @JvmStatic fun main(args: Array<String>) {
            // something..
        }
    }
}

-- So first i've written the kotlin class with main inside it.

Secondly as in your case defined the main class in props in a following way <main.class>org.somepackage.Application</main.class>

After mvn clean install command finished i've got the necessary someapp-1.0-SNAPSHOT-jar-with-dependencies.jar which was able to work.

And at the end please pay attention that you run exactly jar-with-dependencies.jar (not a stripped version like a app-1.0-SNAPSHOT.jar).

Boris Davidov
  • 294
  • 2
  • 6
0

it seems like you have one of the plugins missing. add the bellow to pom.xml file

`<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>
                            yourclassnameKt
                        </mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>`

The plugin will add MANIFEST.mf file, which will tell the Java runtime which class to execute.

There is another issue that has the same problem -> Maven Jar Builder: Could not find or load main class

Reference: https://michaelrice.com/2016/08/hello-world-with-kotlin-and-maven/

Deividas Duda
  • 123
  • 1
  • 8
0

Your POM file is correct, no need to add anything. Just run the generated JAR postfixed with *-jar-with-dependencies.jar in target directory.

Quirino Gervacio
  • 1,240
  • 9
  • 9