简体   繁体   English

无法运行 kotlin 项目

[英]Can't run kotlin project

I am brand new to kotlin.我是 kotlin 的新手。 I have installed kotlin plugins to eclipse.我已经为 Eclipse 安装了 kotlin 插件。 I found the simple example posted below in one of the tutorials.我在其中一个教程中找到了下面发布的简单示例。 The issue is, when I run the project i receive the below stated error.问题是,当我运行项目时,我收到以下错误。

To solve this issue I tried to run the project as kotlin application, but I could not find that option at all.为了解决这个问题,我尝试将项目作为 kotlin 应用程序运行,但我根本找不到那个选项。

please let let me know how to fix this error?请让我知道如何解决此错误?

code :代码

fun main(args : Array) {
println("Hello, World!")
}

error :错误

Error: Could not find or load main class com.example.Main

update :更新

to solve this issue I followed exactly what is in this tutorial and I installed the most recent version of eclipse PHOTON but still the problem is there.为了解决这个问题,我完全按照本教程中的内容进行操作,并安装了最新版本的 eclipse PHOTON,但问题仍然存在。

if your main function is a top level function (not wrapped in a class or object)如果您的 main 函数是顶级函数(未包装在类或对象中)

then the generated class will be FIlenameKt, or for your case MainKt assuming it is in Main.kt那么生成的类将是 FIlenameKt,或者对于你的情况MainKt假设它在Main.kt

It's possible, that the .classpath file is wrong, maybe because you moved your project. .classpath 文件可能是错误的,可能是因为您移动了项目。 I found a solution for your problem here :我在这里找到了解决您问题的方法:

  1. Delete .classpath and .project from your project从你的项目中删除 .classpath 和 .project
  2. Delete your project in eclipse.在 Eclipse 中删除您的项目。 DO NOT check delete project contents on disk.不要检查删除磁盘上的项目内容。
  3. Now, in a file explorer, go into $yourworkspace/.metadata.现在,在文件资源管理器中,进入 $yourworkspace/.metadata。
  4. Search for $yourprojectname搜索 $yourprojectname
  5. Delete everything you find.删除您找到的所有内容。 It should be safe-ish to delete anything in the .metadata directory.删除 .metadata 目录中的任何内容应该是安全的。
  6. In eclipse: File > Import > General > Projects from Folder or Archive > $yourproject > finish在 Eclipse 中:文件 > 导入 > 常规 > 文件夹或存档中的项目 > $yourproject > 完成
  7. Right click your project > properties > Java Build Path > Source tab右键单击您的项目 > 属性 > Java 构建路径 > 源选项卡
  8. Select all source folders, remove.选择所有源文件夹,删除。
  9. Add folder, select src (whatever your src folder is called) and add it添加文件夹,选择 src(不管你的 src 文件夹叫什么)并添加它
  10. Go to libraries tab转到库选项卡
  11. Add any jars to your build path here.在此处将任何 jar 添加到您的构建路径中。 There should be no more errors on your project now.现在您的项目应该不会再有错误了。
  12. Run your project like you normally would.像往常一样运行您的项目。

If you want to test your code, you can also do that online on the Kotlin website here .如果你想测试你的代码,你也可以做到这一点在线科特林网站上这里

I hope this could help you.我希望这可以帮助你。

The main function in Kotlin is different. Kotlin 中的主要功能是不同的。 You need to add <> wrapping the String to the function, and it should look like this:您需要添加 <> 包装字符串到函数,它应该是这样的:

fun main(args : Array<String>) {
    println("Hello, World!")
}

Then your program should compile :-)然后你的程序应该编译:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM