2

In Java the System static method console() returns the object allowing to read from terminal. The Console instance returned by that method provides method readLine() and readPassword(). First one echoing input to the console, but second -- doesn't.

In KotlinJVM application provides top level function readLine() to read input from the console, but there is no one to readPassword(). Also the System.console() returns null.

How I can read password (without echoing) from KotlinJVM application?

I just tried these and got NPE

package com.helpchoice.kotlin.fuel.dsl

fun main(args: Array<String>) {
    try {
        System.console().readPassword("pw:")
    } catch (e: Throwable) {
        println(e::class.java.name)
    }
}
C.A.B.
  • 465
  • 4
  • 12

2 Answers2

3

Apparently this is an IDE problem (see System.console() returns null).

So the only solution recommended: read from System.in if there is no console.

val pw = System.console()?.readPassword() ?: readLine()

This will show your password while debugging from IDE, but will hide if started from console.

C.A.B.
  • 465
  • 4
  • 12
-4

Maybe you can start with creating this:

val console : Console? = System.console()
val password = console.readPassword("Enter password")

Worked for me

Rick Mkl
  • 1
  • 4