1

Referencing Problem when Java Class is used in Kotlin. There is the Java class Base32Decoder.java and code from this class is used in the Kotlin file hello.kt.

When I try to run Java code through a Kotlin file, an error occurs because of no reference could be established to the Java class Base32Decoder.

Error message:

hello.kt:4:25: error: unresolved reference: Base32Decoder

Base32Decoder Java class can't resolve the reference to it. Since this class is used inside the Kotlin file, the reference needs to work.

Code

fun main(args: Array<String>){
    val Base32Decoder = Base32Decoder()
   val rectangleArea: String = Base32Decoder.base32Decode("JBSWY3DPFQQFO33SNRSCC===")
   println("inside the Kotlin codes:" + rectangleArea)
}

How can I reference Java classes when I want to use Java code in Kotlin files?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
EpicAdidash
  • 137
  • 8

1 Answers1

1

The code must be accessible for Kotlin. This means you have to compile (If you compile code in terminal: javac SampleFile.java The Base32Decoder.java file to generate the Base32Decoder.class.

Now generate the JAR-file with a link to the Kotlin file in command line:

jar cf app.jar Base32Decoder.class hello.kt

Now since you use kotlinc you can execute the code in command line like this:

kotlinc -classpath app.jar -script hello.kt

Now your code should run fine. The problem is that Kotlin didn't have access to the Base32Decoder.java class.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
AztecCodes
  • 1,130
  • 7
  • 23