User Tools

Site Tools


tech:se:kotlin:hello-world:hello-world

Table of Contents

Kotlin Hello World

CLI

I downloaded kotlin-compiler-1.3.10.zip, jre-8u191-windows-x64.exe.

If you use Windows Binary Release, and Japanese environment, DO NOT USE kotlin-compiler-1.3.10-windows-x64.zip It has a MS932 code page issue.

Use universal platform independent version release: kotlin-compiler-1.3.10.zip.

PS D:\junk\kotlin> D:\Users\wnogu\Downloads\kotlin-compiler-1.3.10-windows-x64\kotlinc\bin\kotlinc hello.kt
WARN: Failed to load filesystem access layer: Windows 10, 1.8.0_144, nio2=true
java.lang.reflect.InvocationTargetException
        at com.intellij.openapi.util.io.FileSystemUtil$Nio2MediatorImpl.getAttributes(Unknown Source)
        at com.intellij.openapi.util.io.FileSystemUtil.check(Unknown Source)
        at com.intellij.openapi.util.io.FileSystemUtil.getMediator(Unknown Source)
        at com.intellij.openapi.util.io.FileSystemUtil.<clinit>(Unknown Source)
        at com.intellij.openapi.util.io.FileSystemUtil.getAttributes(Unknown Source)
        at com.intellij.openapi.vfs.impl.ZipHandler.setFileAttributes(Unknown Source)
        at com.intellij.openapi.vfs.impl.ZipHandler$1.createAccessor(Unknown Source)
        at com.intellij.openapi.vfs.impl.ZipHandler$1.createAccessor(Unknown Source)
        at com.intellij.util.io.FileAccessorCache.createHandle(Unknown Source)
        at com.intellij.util.io.FileAccessorCache.get(Unknown Source)
        at com.intellij.openapi.vfs.impl.ZipHandler.getCachedZipFileHandle(Unknown Source)
        at com.intellij.openapi.vfs.impl.ZipHandler.acquireZipHandle(Unknown Source)
        at com.intellij.openapi.vfs.impl.ZipHandlerBase.createEntriesMap(Unknown Source)
        at com.intellij.openapi.vfs.impl.ArchiveHandler.getEntriesMap(Unknown Source)
        at com.intellij.openapi.vfs.impl.jar.CoreJarHandler.<init>(Unknown Source)
        at com.intellij.openapi.vfs.impl.jar.CoreJarFileSystem.lambda$new$0(Unknown Source)
        at com.intellij.openapi.vfs.impl.jar.CoreJarFileSystem$$Lambda$52.fun(Unknown Source)
        at com.intellij.util.containers.ConcurrentFactoryMap$3.create(Unknown Source)
        at com.intellij.util.containers.ConcurrentFactoryMap.get(Unknown Source)
        at com.intellij.openapi.vfs.impl.jar.CoreJarFileSystem.findFileByPath(Unknown Source)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment.findJarRoot(Unknown Source)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment.contentRootToVirtualFile(Unknown Source)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment.access$contentRootToVirtualFile(Unknown Source)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment$6.invoke(Unknown Source)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment$6.invoke(Unknown Source)
        at org.jetbrains.kotlin.cli.jvm.compiler.ClasspathRootsResolver.convertClasspathRoots(Unknown Source)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment.<init>(Unknown Source)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment.<unknown>(Unknown Source)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment$Companion.createForProduction(Unknown Source)
        at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.createCoreEnvironment(Unknown Source)
        at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(Unknown Source)
        at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(Unknown Source)
        at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(Unknown Source)
        at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(Unknown Source)
        at org.jetbrains.kotlin.cli.common.CLITool.exec(Unknown Source)
        at org.jetbrains.kotlin.cli.common.CLITool.exec(Unknown Source)
        at org.jetbrains.kotlin.cli.common.CLITool.exec(Unknown Source)
        at org.jetbrains.kotlin.cli.common.CLITool$Companion.doMainNoExit(Unknown Source)
        at org.jetbrains.kotlin.cli.common.CLITool$Companion.doMain(Unknown Source)
        at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler$Companion.main(Unknown Source)
        at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.main(Unknown Source)
Caused by: java.lang.ExceptionInInitializerError
        at sun.nio.fs.Util.split(Unknown Source)
        at sun.nio.fs.WindowsFileSystem.<init>(Unknown Source)
        at sun.nio.fs.WindowsFileSystemProvider.<init>(Unknown Source)
        at sun.reflect.NativeConstructorAccessorImpl.<unknown>(Unknown Source)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at java.lang.Class.newInstance(Unknown Source)
        at sun.nio.fs.DefaultFileSystemProvider.<unknown>(Unknown Source)
        at java.nio.file.FileSystems$DefaultFileSystemHolder.getDefaultProvider(Unknown Source)
        at java.nio.file.FileSystems$DefaultFileSystemHolder.access$000(Unknown Source)
        at java.nio.file.FileSystems$DefaultFileSystemHolder$1.<unknown>(Unknown Source)
        at java.nio.file.FileSystems$DefaultFileSystemHolder$1.<unknown>(Unknown Source)
        at java.security.AccessController.<unknown>(Unknown Source)
        at java.nio.file.FileSystems$DefaultFileSystemHolder.defaultFileSystem(Unknown Source)
        at java.nio.file.FileSystems$DefaultFileSystemHolder.<clinit>(Unknown Source)
        at java.nio.file.FileSystems.getDefault(Unknown Source)
        at java.nio.file.Paths.get(Unknown Source)
        ... 41 more
Caused by: java.nio.charset.UnsupportedCharsetException: MS932
        at java.nio.charset.Charset.forName(Unknown Source)
        at sun.nio.fs.Util.<clinit>(Unknown Source)
        ... 59 more
hello.kt
fun main(args: Array<String>) {
  println("Wataru, Hello, World!!")
}

You should refer official hello world tutorial. Third party documents may have potential stale information. And this wiki, so on.

Let's say Windows PowerShell in 2018 year.

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

PS C:\Users\wnogu> java -cp %KOTLIN_HOME%\lib\*:.\ _DefaultPackage
Error: Could not find or load main class _DefaultPackage
PS C:\Users\wnogu> java -cp %KOTLIN_HOME%\lib\*:.\ _DefaultPackage
Error: Could not find or load main class _DefaultPackage
PS C:\Users\wnogu> cd D:\junk\kotlin
PS D:\junk\kotlin> java -cp %KOTLIN_HOME%\lib\*:.\ _DefaultPackage
Error: Could not find or load main class _DefaultPackage
PS D:\junk\kotlin> echo %KOTLIN_HOME%
%KOTLIN_HOME%
PS D:\junk\kotlin> set

cmdlet Set-Variable at command pipeline position 1
Supply values for the following parameters:
Name[0]: exit
Name[1]: quit
Name[2]: ^D
Name[3]: ^D
Name[4]:
PS D:\junk\kotlin> env
env : The term 'env' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ env
+ ~~~
    + CategoryInfo          : ObjectNotFound: (env:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

PS D:\junk\kotlin> env
env : The term 'env' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ env
+ ~~~
    + CategoryInfo          : ObjectNotFound: (env:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

PS D:\junk\kotlin> echo KOTLIN_HOME
KOTLIN_HOME
PS D:\junk\kotlin> echo $KOTLIN_HOME
PS D:\junk\kotlin> echo $env:KOTLIN_HOME
C:\apps\kotlinc
PS D:\junk\kotlin> java -cp $env:KOTLIN_HOME\lib\*:.\ _DefaultPackage
Error: Could not find or load main class _DefaultPackage
PS D:\junk\kotlin> java -cp $env:KOTLIN_HOME\lib\*:.\ _DefaultPackage
Error: Could not find or load main class _DefaultPackage
PS D:\junk\kotlin> kotlinc hello.kt -include-runtime -d hello.jar

PS D:\junk\kotlin> java -jar hello.jar
Wataru, Hello, World!!
PS D:\junk\kotlin>
PS D:\junk\kotlin>

sounds good.

References

tech/se/kotlin/hello-world/hello-world.txt · Last modified: 2018/11/18 09:48 by wnoguchi