Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Kotlin on Ubuntu?

I just installed Kotlin on Ubuntu with the commands below,

curl -s https://get.sdkman.io | bash
sdk install kotlin

And it's installed with the lines below,

Downloading: kotlin 1.3.61

In progress...

######################################################################################################## 100.0%

Installing: kotlin 1.3.61
Done installing!
*************************************************************

But when I try to execute a file, it doesn't work.

comp@ubuntu:~$ kotlin --version
/home/comp/.sdkman/candidates/kotlin/current/bin/kotlinc: line 80: java: command not found

comp@ubuntu:~$ chmod +x test.kt 
comp@ubuntu:~$ kotlinc test.kt -include-runtime -d hello.jar
/home/comp/.sdkman/candidates/kotlin/current/bin/kotlinc: line 80: java: command not found

comp@ubuntu:~$ which kotlinc
/home/comp/.sdkman/candidates/kotlin/current/bin/kotlinc

Line 80 of the file "/home/comp/.sdkman/candidates/kotlin/current/bin/kotlinc" is below,

"${JAVACMD:=java}" $JAVA_OPTS "${java_args[@]}" -cp "${kotlin_app[@]}" "${kotlin_args[@]}"

What can be the problem? Problems with some Java components? Thanks.

like image 976
sleepy Avatar asked Dec 22 '22 20:12

sleepy


2 Answers

you will also need to install java - try this:

apt install default-jre
like image 106
ligi Avatar answered Dec 25 '22 10:12

ligi


You can use snap to easily install packages and all its dependencies:

sudo snap install --classic kotlin

Now compile using Kotlin compiler kotlinc:

kotlinc test.kt -include-runtime -d test.jar

The -d option indicates the output path for generated class files which may be either a directory or a .jar file. The -include-runtime option makes the resulting .jar file self-contained and runnable by including the Kotlin runtime library in it.

Now run the application:

java -jar test.jar

For any help use:

kotlinc -help

For version info:

kotlin -version
like image 34
sks-15 Avatar answered Dec 25 '22 09:12

sks-15