2

I am now limited to use java command to compile a Kotlin source file.

So after some attempts, I found a kotlin-compiler.jar file in the lib/ folder in kotlinc/.

Then I tried to write a Hello.kt file in kotlinc/lib/:

public fun main(args: Array<String>) {
    println("hello")
}

And then, I executed the command java -jar kotlin-compiler.jar Hello.kt which worked and generated a class file HelloKt.class.

As a result I executed the command java -cp ".:kotlin-runtime.jar" HelloKt, and it also worked:

[xxxxxxx@uss lib]$ java -cp ".:kotlin-runtime.jar" HelloKt
hello

But, for a more complicated source file:

AimToTen.kt:

class AimToTen() {
    fun need(marks: Array<Int>): Int {
        // multiply 10 first
        for (idx: Int in marks.indices) marks[idx] *= 10

        val result: Int = 190 * marks.size - 2 * marks.sum();
        return when {
            result < 0 -> 0
            result % 10 != 0 -> result / 10 + 1
            else -> result / 10;
        }
    }
}

I tried to execute java -jar kotlin-compiler.jar AimToTen.kt, but some functions missed(?):

[xxxxxxx@uss intro1]$ java -jar kotlin-compiler.jar AimToTen.kt
AimToTen.kt:4:32: error: unresolved reference: indices
        for (idx: Int in marks.indices) marks[idx] *= 10
                               ^
AimToTen.kt:10:56: error: unresolved reference: sum
        val result: Int = 190 * marks.size - 2 * marks.sum();
                                                       ^

So I tried again:

[xxxxxxx@uss intro1]$ java -jar kotlin-compiler.jar
Welcome to Kotlin version 1.0.3 (JRE 1.8.0_65-b17)
Type :help for help, :quit for quit
>>> println(123)
error: unresolved reference: println
println(123)
^

>>> var test: IntArray = intArrayOf(1, 2)
>>> test
[I@7d64e326
>>>

Also, in other folder, the Hello.kt cannot be compiled even if I copied all the .jar files in the same folder.


Ehh, I do appreciate if you can help me with this issue! I want to compile this file with java command, without any absolute paths.

P.S. Using kotlinc AimToTen.kt works.

2
  • you are missing kotlin-stdlib somewhere. Try adding it to the compiler options Commented Aug 28, 2016 at 22:28
  • @voddan That's what I thought, but how to add it? The stdlib you mentioned seems to be kotlin-runtime.jar, which I've already tried: java -cp kotlin-runtime.jar -jar kotlin-compiler.jar AimToTen.kt and java -cp ".:kotlin-runtime.jar" -jar kotlin-compiler.jar AimToTen.kt resulted in failure. Thanks for answering! Commented Aug 28, 2016 at 23:43

1 Answer 1

1

As stated in the docs, the line to compile a Kotlin file is

$ kotlinc hello.kt -include-runtime -d hello.jar

BTW, if you are trying to learn/try out Kotlin in command line (which you should not!), you may consider http://try.kotlinlang.org/

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but scala and clojure are all able to be compiled by java command. So I think Kotlin should be as well. Additionally, the kotlinc is a bash file and what command it's using is actually java xxxxxxxxx.
@MewX Use the script. That's what it's for.
@EJP Thanks, but why the script needs the installed path? I was wondering what files are required in the compiling procedure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.