尝试从Java实例化Kotlin类时出错 [英] Error trying to instantiate a Kotlin class from Java
问题描述
我试图从Java实例化Kotlin类,但是每次尝试用Maven编译时,都会出现错误cannot find symbol
:
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<String>): Boolean {
if (args.isEmpty()) {
if (sender !is Player)
throw NoConsoleAccessException()
sender.openInventory(ConfigGUI(game).build())
return true
}
return false
}
}
不确定为什么格式不正确,但是无论如何,在将其转换为可以正常工作的Kotlin类之前,我需要在我的主类Java类中注册此命令.当我尝试从Java类实例化Kotlin类时,IDE中没有错误,但是当我编译它时,Maven发出了尖叫声
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
推荐答案
我仍在弄清楚Kotlin,但我尝试根据您的示例尝试一些排列.我很容易根据您的Hastebin pom来创建您的问题.
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.
您是对的,将<phase>
更改为process-sources
可以使我的测试代码在Maven中进行编译,但是(诚实地)我无法永远记住所有Maven阶段,因此我不会个人无需进行更多研究就可以轻松地转换为过程源,特别是因为IntelliJ工具依赖于compile
阶段.
You were right that changing the <phase>
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.
在研究了我自己的互操作示例之后,关键的部分(并且在工具默认设置中缺少)似乎是<sourceDirectory>
元素,它是<build>
的顶级子元素,如下所示:
After playing around with my own interop examples, it seems like the critical (and missing from the tooling defaults) piece is the <sourceDirectory>
element as a top-level child of <build>
as in:
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
在运行mvn compile
终端命令时,将<sourceDirectory>
作为顶级元素添加到<build>
下,使我的Java到Kotlin互操作代码得以编译.当我在"java"目录中混合源文件以同时包含Java和Kotlin文件时,确实如此.
Adding the <sourceDirectory>
as a top-level element under <build>
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.
作为旁注(我不明白为什么写这篇文章),当我将"Kotlin"作为类名的一部分添加到Kotlin源中时,我不需要添加<sourceDirectory>
元素...
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 <sourceDirectory>
element...
这篇关于尝试从Java实例化Kotlin类时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!