2017-05-07 46 views
0
實例化一個科特林類

我試圖實例從Java科特林類,但每一次我嘗試使用Maven編譯我的錯誤cannot find symbol錯誤試圖從Java

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

不知道爲什麼沒」 t格式,但無論如何,在我將它轉換爲Kotlin類之前,我需要在我的主類(它是一個Java類)中註冊該命令。當我試圖從一個Java類實例化一個科特林類有在IDE中沒有錯誤,但是當我去編譯Maven的尖叫

cannot find symbol 
[ERROR] symbol: class ConfigCommand 
+0

你能否提供一些更多的上下文和編譯文件的代碼?也許甚至提供一個最小的失敗例子? –

+0

@ChristianBrüggemann這樣做有幫助還是需要更多? –

+0

你是否在你的'pom.xml'中配置了Kotlin Maven插件[詳見這裏](https://kotlinlang.org/docs/reference/using-maven.html)?如果是,你的'pom.xml'看起來像什麼? 順便說一句,在StackOverflow上的代碼格式是使用4空格縮進完成的。 –

回答

0

我還在搞清楚Kotlin,但我試圖通過基於您的示例的一些排列組合。我很容易根據你的哈斯賓斌創建你的問題。

你說得對,將<phase>更改爲process-sources讓我的測試代碼在Maven中編譯,但是我(老實說)不能始終記住所有的Maven階段,因此我不會親自感到舒服在沒有更多研究的情況下轉換爲過程源 - 尤其是因爲IntelliJ工具依賴於compile階段。

用我自己的互操作實例玩耍後,它似乎是關鍵(從模具默認丟失)的一塊是<sourceDirectory>元素爲<build>頂級孩子在:

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

添加作爲<build>下的頂級元素的<sourceDirectory>在我運行mvn compile終端命令時將我的Java編譯爲Kotlin互操作代碼。當我將「java」目錄中的源文件混合爲包含Java和Kotlin文件時,情況就是如此。當我在我的Kotlin源文件中添加「Kotlin」作爲我的課程名稱的一部分時,我不需要添加<sourceDirectory>元素。作爲一個附註(我不明白爲什麼當我寫這篇文章時),我不需要添加<sourceDirectory>元素。 ...

1

去低谷this頁面多次,意識到我沒有做錯任何事情之後我開始只是弄亂了我的pom,並最終通過改變Kotlin編譯的階段來工作。process-sources

0

maven-compiler-plugin添加了在相應階段中首先運行的默認執行default-compiledefault-testCompile

爲了在Java代碼中使用Kotlin類,您需要在運行Java編譯器之前運行Kotlin編譯器。一種方法是將其執行時間安排到process-sources階段。另一種方法是在執行Kotlin插件之後「取消調度」Java插件的默認執行並安排新的執行。

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

      ... 

     </executions> 
    </plugin> 

然後執行新添加:

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

完整的示例如下所示:

Java插件的默認執行與pom.xml中的這一部分關閉https://kotlinlang.org/docs/reference/using-maven.html#compiling-kotlin-and-java-sources