1

laden Versuch einfachste Kotlin Hallo Welt möglich:Fehler: konnte nicht gefunden oder Hauptklasse Hello.class

[email protected]:~/kotlin$ 
[email protected]:~/kotlin$ ll 
total 32 
drwxr-xr-x 2 thufir thufir 4096 Oct 27 07:28 ./ 
drwx------ 46 thufir thufir 16384 Oct 27 06:47 ../ 
-rw-r--r-- 1 thufir thufir 104 Oct 27 07:27 Hello.kt 
[email protected]:~/kotlin$ 
[email protected]:~/kotlin$ kotlinc Hello.kt 
WARNING: An illegal reflective access operation has occurred 
WARNING: Illegal reflective access by com.intellij.util.text.StringFactory to constructor java.lang.String(char[],boolean) 
WARNING: Please consider reporting this to the maintainers of com.intellij.util.text.StringFactory 
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations 
WARNING: All illegal access operations will be denied in a future release 
[email protected]:~/kotlin$ 
[email protected]:~/kotlin$ kotlin Hello.class 
error: could not find or load main class Hello.class 
[email protected]:~/kotlin$ 
[email protected]:~/kotlin$ cat Hello.kt 
class Hello { 



    fun main(args: Array<String>) { 
     println("Hello, world!" + args[0]) 
    } 
} 
[email protected]:~/kotlin$ 
[email protected]:~/kotlin$ kotlinc -version 
info: kotlinc-jvm 1.1.51 (JRE 9.0.0.15+181) 
[email protected]:~/kotlin$ 

Wie führe ich aus der CLI das?

gewünschte Ausgabe:

[email protected]:~/kotlin$ 
[email protected]:~/kotlin$ kotlinc 
Welcome to Kotlin version 1.1.51 (JRE 9.0.0.15+181) 
Type :help for help, :quit for quit 
>>> 
>>> println("hello world"); 
WARNING: An illegal reflective access operation has occurred 
WARNING: Illegal reflective access by com.intellij.util.text.StringFactory to constructor java.lang.String(char[],boolean) 
WARNING: Please consider reporting this to the maintainers of com.intellij.util.text.StringFactory 
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations 
WARNING: All illegal access operations will be denied in a future release 
hello world 
>>> 
>>> :quit 
[email protected]:~/kotlin$ 

Antwort

1

Zunächst einmal, dass Hello möglicherweise nicht wie erwartet funktioniert, weil es main Funktion nicht statisch ist. In Kotlin benötigen Sie keine Klasse, um eine main Methode zu definieren. Nur Funktionen nutzen:

fun main(args: Array<String>) { 
    println("Hello, world!" + args[0]) 
} 

Dann nach dem Kompilieren, sollten Sie es nicht wie kotlin <File>.class nennen, aber nur kotlin <File>, die .class Suffix ist redundant:

$ ./compiler/kotlinc/bin/kotlin HelloKt test 
Hello, world!test 
Verwandte Themen