Kotlin + Maven can not find main class when running packaged jar
Asked Answered
D

2

6

I am trying to package(maven package) and run(java -jar file.jar) a project that contains both java and kotlin code. I have followed the tutorial at: https://michaelrice.com/2016/08/hello-world-with-kotlin-and-maven/

kotlin source src/main/kotlin java source src/main/java

the file I am trying to run is located in src/main/kotlin/THEFILE.kt

After succesfull packaging trying to run the jar I get an error Error: Could not find or load main class THEFILEKt

What can be the reason for this and how to fix it?

Thanks in advance!!!

Pom.xml includes a necessary kotlin plugins and dependency:

<dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-stdlib-jdk8</artifactId>
        <version>${kotlin.version}</version>
</dependency>


<plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </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>
                    <configuration>
                        <sourceDirs>
                            <source>src/main/java</source>
                            <source>src/main/kotlin</source>
                        </sourceDirs>
                    </configuration>
                </execution>
                <execution>
                    <id>test-compile</id>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <jvmTarget>1.8</jvmTarget>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <executions>

                <execution>
                    <id>default-compile</id>
                    <phase>none</phase>
                </execution>

                <execution>
                    <id>default-testCompile</id>
                    <phase>none</phase>
                </execution>
                <execution>
                    <id>java-compile</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>java-test-compile</id>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>compile</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>testCompile</id>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>testCompile</goal>
                    </goals>
                </execution>


            </executions>
        </plugin>

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

                        </mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>



        <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>
                                    THEFILEKt
                                </mainClass>
                            </manifest>
                        </archive>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                </execution>
            </executions>
        </plugin>
Daiseydaisi answered 14/3, 2018 at 22:40 Comment(3)
THEFILEKT – the Kotlin file facade class names end with Kt, not KT. Please try to replace THEFILEKT with THEFILEKt throughout the POM.Braithwaite
The file name does not matter to much. The content is more important. What is CsiidGenerator? How does the generated manifest look like?Hoem
Manifest file: Manifest-Version: 1.0 Archiver-Version: Plexus Archiver Built-By: deividas Created-By: Apache Maven 3.5.0 Build-Jdk: 1.8.0_131 Main-Class: THEFILEDaiseydaisi
A
1

There are some issues when you have both java and kotlin code. I noticed the following -

  • If I had a folder called src/main/java, then the main class needed to be written in Java.

  • If I had a folder called src/main/kotlin, then the main class needed to be written in kotlin.

I did not try with both java and kotlin source folders.

I would suggest that you move all your java code to the kotlin folder. It will still work. Perhaps when there is a java folder, it expects the main class to be in java? Not sure.

Achievement answered 9/7, 2018 at 15:58 Comment(1)
If you have your security beans in java, do keep your main class in java too, and vice versa. I haven't researched into the underlying reasons, but having written all of my security related code in java, it worked only after I renamed the kotlin folder to java, and moved my application class to java. Models etc are written in kotlin, but they continue to work.Achievement
C
-1

I had the same problem until I deleted maven-compiler-plugin. pom.xml build block:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <useSystemClassLoader>false</useSystemClassLoader>
            </configuration>
        </plugin>

        <!-- lombok -->
        <plugin>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok-maven-plugin</artifactId>
            <version>1.16.8.0</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>delombok</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <!-- instead of default compiler -->
        <plugin>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-plugin</artifactId>
            <version>${kotlin.version}</version>
            <executions>
                <execution>
                    <id>compile</id>
                    <phase>process-sources</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                    <configuration>
                        <sourceDirs>
                            <source>src/main/java</source>
                            <source>src/main/kotlin</source>
                            <source>target/generated-sources/annotations</source>
                        </sourceDirs>
                    </configuration>
                </execution>
                <execution>
                    <id>test-compile</id>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>test-compile</goal>
                    </goals>
                    <configuration>
                        <sourceDirs>
                            <source>src/test/java</source>
                            <source>src/test/kotlin</source>
                        </sourceDirs>
                    </configuration>
                </execution>
            </executions>
            <configuration>
                <jvmTarget>1.8</jvmTarget>
            </configuration>
        </plugin>
    </plugins>
</build>
Chiliasm answered 23/12, 2018 at 7:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.