Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

230
Visualizações
Specifying default arguments for main

What am I doing wrong here?

fun main(args: Array<String> = arrayOf("abc")) {
    val a = args[0]
}

compiles fine but:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

I want to make it optional to have an argument and supply a default value if none was specified

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

This probably does not work because of how the underlying Java platform works and how Kotlin translates default arguments to something the JVM can understand. You could say it's a bug in Kotlin...

The Java virtual machine looks for a method with the following signature to run your program (this is Java):

public static void main(String[] args)

Note that default argument values do not exist in Java, so if you declare a method with default values in Kotlin, the Kotlin compiler generates multiple methods in the Java byte code that will look like this when converted to Java:

public static void main(String[] args) {
    // the normal main method
}

// generated because you have a method with default values
public static void main() {
    main(new String[]{ "abc" });
}

When you run the program, the JVM will still look for the method with the String[] args argument. It will ignore the method with no arguments, that was generated for the Kotlin main method with default arguments.

So it's always going to call the String[] args version, even if there are no arguments on the command line - leading to the error that you get.

Workaround: Don't use default values for the main method; check if arguments were given inside the method:

fun main(args: Array<String>) {
    val realArgs = if (args.size > 0) args else arrayOf("abc")

    // work with realArgs
}
over 4 years ago · Santiago Trujillo Relatório

0

I think it's a question of signature the JVM's search for main methods: a main method should have one parameter of String[] type, and this parameter receives value(s) from CLI; if the CLI does not provide any parameter, the main method receives a new String[0], which causes the error you're experiencing.

I think you can solve the problem slightly changing the approach:

fun main(args: Array<String>) {
    println(args)
    val a = args.getOrElse(0) {"abc"}
    println(a)
}

should work for your scenario: if there is no parameter with index 0 a gets the default value you passed (as a lambda) to getOrElse.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda