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

查看:246
本文介绍了如何从命令行运行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:\Users\User\Desktop>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:\Users\User\Desktop>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:\Users\User\Desktop>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 Application插件从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添加到提供的类路径中,然后运行该类.您可能需要添加其他从Java运行"部分中提到的Kotlin库.

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来为您解决此问题(请参见

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天全站免登陆