Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
like image 518
Deividas Duda Avatar asked Nov 17 '22 17:11

Deividas Duda


1 Answers

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.

like image 142
Abhinav Manchanda Avatar answered Dec 15 '22 03:12

Abhinav Manchanda