如何从命令行运行 Kotlin 类? [英] How to run Kotlin class from the command line?

查看:33
本文介绍了如何从命令行运行 Kotlin 类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道之前有人问过这个问题,但那里的信息都没有帮助我.

I understand this question has been asked before, but none of the information there has helped me.

这是我的情况:我无法运行已编译的 Kotlin 类.当我尝试像运行普通 Java 类一样运行它时,我得到以下信息:

Here is my situation: I can't run a compiled Kotlin class. When I try to run it like I would a normal java class I get the following:

C:UsersUserDesktop>java _DefaultPackage

Exception in thread "main" java.lang.NoClassDefFoundError: jet/runtime/Intrinsics
    at _DefaultPackage.main(Finder.kt)
Caused by: java.lang.ClassNotFoundException: jet.runtime.Intrinsics
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    ... 1 more

这让我相信 Kotlin 运行时只是缺失,考虑到输出.所以我尝试了以下方法:

This led me to believe that the Kotlin runtime was simply missing, given that output. So I tried the following:

C:UsersUserDesktop>java -cp kotlin-runtime.jar _DefaultPackage

Error: Could not find or load main class _DefaultPackage

这让我觉得也许我需要将类文件添加到我声明的类路径中:

Which made me think that maybe I needed to add the class file to my declared classpath so:

C:UsersUserDesktop>java -cp kotlin-runtime.jar';_DefaultPackage.class _DefaultPackage

Error: Could not find or load main class _DefaultPackage

我错过了什么?

推荐答案

了解主类的名称

要运行 Kotlin 类,您实际上是在运行一个在文件级别创建的特殊类,该类包含您的 main() 和其他顶级函数(在类或接口之外)).因此,如果您的代码是:

Knowing the Name of Your Main Class

To run a Kotlin class you are actually running a special class that is created at the file level that hold your main() and other functions that are top-level (outside of a class or interface). So if your code is:

// file App.kt
package com.my.stuff

fun main(args: Array<String>) {
  ...
}

然后您可以通过运行com.my.stuff.AppKt 类来执行程序.此名称源自您的文件名,并附加了 Kt.您可以通过添加此 文件来更改文件中此类的名称-有针对性的注释:

Then you can execute the program by running the com.my.stuff.AppKt class. This name is derived from your filename with Kt appended. You can change the name of this class within the file by adding this file-targeted annotation:

@file:JvmName("Foo")  

您还可以将您的 main() 放入一个带有伴随对象的类中,并使用 JvmStatic 注释使其成为 static.因此,您的班级名称是您选择的名称:

You can also put your main() into a class with a companion object and make it static using the JvmStatic annotation. Therefore your class name is the one you chose:

// file App.kt
package com.my.stuff

class MyApp {
    companion object {
        @JvmStatic fun main(args: Array<String>) {
          ...
        }
    }
}

现在您只需运行 com.my.stuff.MyApp

您需要您的应用程序 JAR 和任何依赖项.对于在 Maven/Gradle 之外的 Kotlin 特定 JAR,您需要一个 Kotlin 发行版,其中包含:

You need your application JAR and any dependencies. For Kotlin specific JARs when outside of Maven/Gradle you need a Kotlin distribution which contains:

  • kotlin-stdlib.jar(标准库)
  • kotlin-reflect.jar 仅当使用 Kotlin 反射时
  • kotlin-test.jar 用于使用 Kotlin 断言类的单元测试
  • kotlin-stdlib.jar (the standard library)
  • kotlin-reflect.jar only if using Kotlin reflection
  • kotlin-test.jar for unit tests that use Kotlin assertion classes

如果在 Intellij 中(如果它是您的 IDE),您可以右键单击 main() 函数并选择运行,它将为您创建一个运行时配置并显示完全限定的类名将会被使用.如果您不确定生成的类的名称,您可以随时使用它.

If in Intellij (if it is your IDE) you can right click on the main() function and select Run, it will create a runtime configuration for you and show the fully qualified class name that will be used. You can always use that if you are unsure of the name of the generated class.

您还可以使用 Gradle 应用程序插件从 Gradle 运行进程,或者创建一个可运行的系统,其中包含 JAR 及其所有依赖项的 zip/tgz,以及一个启动脚本.使用上面的示例类,您可以将其添加到您的 build.gradle 中:

You can also use the Gradle Application plugin to run a process from Gradle, or to create a runnable system that includes a zip/tgz of your JAR and all of its dependencies, and a startup script. Using the example class above, you would add this to your build.gradle:

apply plugin: 'application'

mainClassName = 'com.my.stuff.AppKt'

// optional:  add one string per argument you want as the default JVM args
applicationDefaultJvmArgs = ["-Xms512m", "-Xmx1g"] 

然后从命令行使用:

// run the program
gradle run

// debug the program
gradle run --debug-jvm

// create a distribution (distTar, distZip, installDist, ...)
gradle distTar

直接从 Java 命令行运行

如果您有一个可运行的 JAR,并假设 KOTLIN_LIB 指向 Kotlin 运行时库文件所在的目录:

Running Directly from Java Command-line

If you have a runnable JAR, and assuming KOTLIN_LIB points to a directory where Kotlin runtime library files reside:

java -cp $KOTLIN_LIB/kotlin-stdlib.jar:MyApp.jar com.my.stuff.AppKt

请参阅上面有关您可能需要的其他 JAR 文件的说明.如果您有一个可运行的 JAR(清单指向 com.my.stuff.AppKt 作为主类),则略有不同:

See the notes above about other JAR files you might need. A slight variation if you have a runnable JAR (with the manifest pointing at com.my.stuff.AppKt as the main class):

java -cp $KOTLIN_LIB/kotlin-stdlib.jar -jar MyApp.jar

使用 Kotlin 命令行工具运行

如果您通过 Homebrew 或其他包管理器安装 Kotlin 工具.(在 Mac OS X brew update ; brew install kotlin) 然后运行起来很简单:

Running using the Kotlin command-line tool

If you install Kotlin tools via Homebrew or other package manager. (on Mac OS X brew update ; brew install kotlin) Then it is very simple to run:

kotlin -cp MyApp.jar com.my.stuff.AppKt

此命令将 stdlib 添加到提供的类路径中,然后运行该类.您可能需要添加额外的 Kotlin 库,如上文从 Java 运行"部分所述.

This command adds the stdlib to the classpath provided, then runs the class. You may need to add additional Kotlin libraries as mentioned in the section above "Running from Java."

这并不常见,因为大多数人使用其他构建工具,但是 Kotlin 编译器可以创建一个可运行的 Jar 来为您解决这个问题(请参阅 http://kotlinlang.org/docs/tutorials/command-line.html) 将运行时和您的代码捆绑在一起.尽管这在使用 Maven 和 Gradle 等工具或 IDE 构建时并不常见.然后使用普通的 Java 运行:

This is not very common since most people use other build tools, but the Kotlin compiler can create a runnable Jar that solves this for you (see http://kotlinlang.org/docs/tutorials/command-line.html) when it bundles the runtime and your code together. Although this isn't as common when using tools such as Maven and Gradle, or IDE builds. Then run using the normal Java:

java -jar MyApp.jar

这篇关于如何从命令行运行 Kotlin 类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆