Kotlin + Maven 在运行打包的 jar 时找不到主类

Dei*_*uda 5 java jar executable-jar maven kotlin

我正在尝试打包(maven 包)并运行(java -jar file.jar)一个包含 java 和 kotlin 代码的项目。我遵循了教程:https : //michaelrice.com/2016/08/hello-world-with-kotlin-and-maven/

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

我试图运行的文件位于 src/main/kotlin/THEFILE.kt

在成功打包尝试运行 jar 后,我收到一个错误 Error: Could not find or load main class THEFILEKt

这可能是什么原因以及如何解决?

提前致谢!!!

Pom.xml 包含必要的 kotlin 插件和依赖项:

<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>
Run Code Online (Sandbox Code Playgroud)

Abh*_*nda 1

当您同时拥有 java 和 kotlin 代码时,会出现一些问题。我注意到以下几点——

  • 如果我有一个名为 src/main/java 的文件夹,那么主类需要用 Java 编写。

  • 如果我有一个名为 src/main/kotlin 的文件夹,那么主类需要用 kotlin 编写。

我没有尝试同时使用 java 和 kotlin 源文件夹。

我建议您将所有 java 代码移至 kotlin 文件夹。它仍然会起作用。也许当有一个java文件夹时,它期望主类在java中?没有把握。