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