You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kotlin转译类在Maven构建时出现“找不到符号”错误求助

问题:Java调用转译后的Kotlin类,Maven编译报"cannot find symbol"

项目混合使用Java和Kotlin,通过IntelliJ IDEA的「将Java文件转换为Kotlin文件」功能转译了部分Java类,但执行Maven构建时,所有使用这些转译类的地方都出现cannot find symbol错误,导入语句也报错。

转译后的Kotlin类

open class ListConverter<AnyTypeList> {

    private val objectMapper = ObjectMapper()
        .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) // ISODate
        .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
        .registerModule(JavaTimeModule())
        .registerModule(ParameterNamesModule())
        .registerModule(Jdk8Module())
        .registerModule(KotlinModule())

    fun transformToType(attributeValue: AttributeValue): List<AnyTypeList> {
        try {
            val json = attributeValue.s()
            return objectMapper.readValue(json, object : TypeReference<List<AnyTypeList>>() {})
        } catch (e: JsonProcessingException) {
            throw RuntimeException("Failed to convert AttributeValue to list ", e)
        }
    }

    fun transformFromType(inputs: List<AnyTypeList>?): AttributeValue {
        return if (inputs == null) {
            AttributeValue.builder().nul(true).build()
        } else try {
            val json = objectMapper.writeValueAsString(inputs)
            AttributeValue.builder().s(json).build()
        } catch (e: JsonProcessingException) {
            throw RuntimeException("Failed to convert list to AttributeValue", e)
        }
    } 
}

Java中使用该类的代码

public class AConverter extends AnyTypeListConverter<A> {
    
    public List<A> transformTo(B attributeValue) {
        return transformToType(attributeValue);
    } 
}

编译错误信息

[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :  [INFO]
-------------------------------------------------------------   
[ERROR] /C:/model/a/AConverter.java:[5,12] cannot find symbol

解决方案

1. 修正类名匹配错误

Java代码里继承的AnyTypeListConverter和Kotlin定义的ListConverter类名完全不一致,直接修改Java类的继承语句:

// 修改前
public class AConverter extends AnyTypeListConverter<A> {
// 修改后
public class AConverter extends ListConverter<A> {

2. 配置Maven编译顺序(核心解决编译找不到类的问题)

混合Java和Kotlin的Maven项目,默认会先编译Java再编译Kotlin,导致Java代码找不到Kotlin类。需要在pom.xml中配置kotlin-maven-plugin,确保先编译Kotlin:

在<build>的<plugins>节点中添加以下配置:

<plugin>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-maven-plugin</artifactId>
    <version>${kotlin.version}</version> <!-- 替换为你项目使用的Kotlin版本 -->
    <executions>
        <execution>
            <id>compile</id>
            <phase>process-sources</phase>
            <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>
            <phase>process-test-sources</phase>
            <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>
    <dependencies>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-allopen</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
    </dependencies>
</plugin>

同时确保项目已添加kotlin-stdlib依赖:

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

3. 检查包路径与导入语句

确认Kotlin类的包路径和Java类的导入语句完全一致,比如Kotlin类在com.example.converter包下,Java类需要添加导入:

import com.example.converter.ListConverter;

4. 修正方法参数类型不匹配

Java代码中transformTo方法的参数是B类型,但Kotlin的transformToType需要的是AttributeValue类型。如果B不是AttributeValue的子类,需要修改参数类型或做类型转换:

// 直接修改参数类型为AttributeValue
public List<A> transformTo(AttributeValue attributeValue) {
    return transformToType(attributeValue);
}

5. 清理Maven缓存并重新构建

执行以下命令清理缓存后重新编译,避免旧编译残留影响:

mvn clean install

内容的提问来源于stack exchange,提问作者JITHIN_PATHROSE

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 20:34:55