Java is fully compatible with Kotlin . So to call java code in kotlin first write following java and kotlin code two separate file .
KotlinApplication.kt
fun main(args:Array<String>){
println("Kotlin Application Started ....... ");
var javaApplication = JavaApplication();
println("-------------------------------------------");
println("Navigating to Java Application ....... ");
println("-------------------------------------------");
javaApplication.naivgateToJavaApplication();
}
JavaApplication.java
public class JavaApplication{
public JavaApplication(){}
public static void main(String []args){
System.out.println("Java Application Started ..... ");
}
public void naivgateToJavaApplication(){
System.out.println("Welcome in Java Application .......");
}
}
To Compile and run above two file there is two method .
First Method to Compile and run java code in kotlin application
Compile using kotlinc and Running using Kotlin
1 . Complie java file
javac JavaApplication.java
2 .Running java file
java JavaApplication
3.Compling kotlin file by setting class path of JavaApplication.class file
kotlinc -cp . KotlinApplication.kt
Note :-
After Compiling of KotlinApplication.kt kotlinc compiler conventionally generate
KotlinApplicationKt.class
by default kotlinc compiler append Kt in suffix of kotlin file name.
4. Now at last Runing KotlinApplicationKt.class by seting class path of JavaApplication.class file
kotlin -cp . KotlinApplicationKt
Output in Console ......
Second Method to Compile and run java code in kotlin application
Complie and package into java jar using kotlinc and Runing java jar using Java
Running with Java Jar using Java
1 .Compile java file
javac JavaApplication.java
2 .Running java file
java JavaApplication
3.Compling kotlin file by setting class path of JavaApplication.class file
kotlinc -cp . KotlinApplication.kt -include-runtime -d kotlinapplication.jar
4. Updating kotlinapplication.jar with JavaApplication.class file
jar -uf KotlinApplication.jar JavaApplication.class
5. Running KotlinApplication.jar file
java -jar KotlinApplication.jar
Output in Console .....
No comments:
Post a Comment