java实例化出错,尝试从Java实例化Kotlin类时出错

I am trying to instantiate a Kotlin class from Java but every time I try to compile with Maven I get the error cannot find symbol:

class ConfigCommand(private val game: Game) : Command("config", "") {

init {

addAliases("cfg")

}

override fun getRequiredRank(): Rank? {

return null

}

override fun getDescription(): String {

return "Shows the config for the game"

}

@Throws(CommandException::class)

override fun execute(sender: CommandSender, args: Array): Boolean {

if (args.isEmpty()) {

if (sender !is Player)

throw NoConsoleAccessException()

sender.openInventory(ConfigGUI(game).build())

return true

}

return false

}

}

Not sure why that didn't format correctly but anyway before I converted it to a Kotlin class it worked but I need to register this command in my main class which is a Java class. When I try to instantiate a Kotlin class from a Java class there are no errors in the IDE but when I go to compile it maven screams

cannot find symbol

[ERROR] symbol: class ConfigCommand

解决方案

I'm still figuring out Kotlin, but I tried to work through a few permutations based on your example. I was easily able to create your problem based on your Hastebin pom.

You were right that changing the to process-sources got my test code to compile in Maven, but I (honestly) can't always remember all the Maven phases off the top of my head so I wouldn't personally be comfortable with changing to process-sources without more research -- especially since the IntelliJ tooling relies on the compile phase.

After playing around with my own interop examples, it seems like the critical (and missing from the tooling defaults) piece is the element as a top-level child of as in:

src/main/java

org.jetbrains.kotlin

kotlin-maven-plugin

${kotlin.version}

compile

Adding the as a top-level element under made my Java to Kotlin interop code compile when I ran the mvn compile terminal command. That was true when I mixed the source files in the "java" directory to contain both Java and Kotlin files.

As a side note (and I don't I understand why as I write this), when I added "Kotlin" as part of my class name to my Kotlin source I didn't need to add the element...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值