How to convert Java source files into Kotlin in an existing project using command line?


I am trying to convert some of the java files into kotlin using command line. Is there any library available to convert the java file into kotlin?


Java to Kotlin conversion cannot be implemented correctly outside of Intellij Idea.

You can use JavaToKotlinTranslatorKt.translateToKotlin function from kotlin-plugin.jar, but you will need to have a mock implementation of Intellij Idea Project (see example) and this conversion will be different from the result of ‘Convert Java to Kotlin’ Action in Intellij Idea.


Thanks. I will try.


Maybe it would be helpful, if IntelliJ would allow to convert more than one file at a time.


It is posiible - just select them is Project View and invoke Convert Java to Kotlin Action


Go to Android studio main_Activity.
Enter CTRL + ALT + SHIFT + K.
The Java code will be converted to Kotlin.