java - 在 Java : Cannot find symbol 中使用 Kotlin 类

标签 java maven build interop kotlin

我找到了this关于 Android 的类似问题,但我使用纯 Java 和 Maven 作为构建工具。我认为最好发布一个新问题。

我创建了一个 Kotlin 类,我试图从 Java 类中将其引用为 MyKotlinClass.class。 Maven 构建失败,而 IntelliJ Idea 中的编译工作正常。我已经在 maven 中添加了 Kotlin 插件:

        <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>

但这无济于事:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project app: Compilation failure
[ERROR] MyClassLinkingKotlin.java:[100,40] error: cannot find symbol

行/列完全引用符号MyKotlinClass.class。即使这样使用也会失败:

System.err.println(MyKotlinClass.class)

最佳答案

您的 Maven 配置添加了 Kotlin 编译器插件,但没有调整 Java 编译器插件的执行,以便 Java 编译器在 Kotlin 编译器之后运行。因此,Java 编译器在 Kotlin 之前运行,并且看不到 Kotlin 编译的类。

这是一个显示混合语言项目正确配置的片段(取自 the documentation):

<build>
    <plugins>
        <plugin>
            <artifactId>kotlin-maven-plugin</artifactId>
            <groupId>org.jetbrains.kotlin</groupId>
            <version>${kotlin.version}</version>
            <executions>
                <execution>
                    <id>compile</id>
                    <goals> <goal>compile</goal> </goals>
                    <configuration>
                        <sourceDirs>
                            <sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
                            <sourceDir>${project.basedir}/src/main/java</sourceDir>
                        </sourceDirs>
                    </configuration>
                </execution>
                <execution>
                    <id>test-compile</id>
                    <goals> <goal>test-compile</goal> </goals>
                    <configuration>
                        <sourceDirs>
                            <sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
                            <sourceDir>${project.basedir}/src/test/java</sourceDir>
                        </sourceDirs>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <executions>
                <!-- Replacing default-compile as it is treated specially by maven -->
                <execution>
                    <id>default-compile</id>
                    <phase>none</phase>
                </execution>
                <!-- Replacing default-testCompile as it is treated specially by maven -->
                <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>
            </executions>
        </plugin>
    </plugins>
</build>

关于java - 在 Java : Cannot find symbol 中使用 Kotlin 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43980110/

相关文章:

java - 如何在 Java 中以编程方式解析 PKCS12 文件?

java - java中的系统类加载器有什么用?

python - 使用 Python 3.3 在 OSX 上使用 MKL 构建 NumPy 时出错

testing - TFS 构建服务器不创建测试结果

java - 用户注册表单 Spring MVC

java - 从相应索引处的元素的数组创建列表

java - 如何在maven项目中构建包含jar包的war

java - 如何在使用 maven 构建的 spring boot 2 java 应用程序中使用 groovy 解释(带有 spring-aop 注释)?

Maven 无法解析依赖关系 spark

build - 在Mac OS X上构建Z3