在 Eclipse 中为 Scala 配置运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4398676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 19:48:18  来源:igfitidea点击:

configure run in eclipse for Scala

eclipsescala

提问by raghav

I am a beginner in Scala. I installed Scala IDE in eclipse and now I want to run my application programme. It never shows "run as Scala application", instead it shows "run as Java application" or "Java applet"

我是 Scala 的初学者。我在 Eclipse 中安装了 Scala IDE,现在我想运行我的应用程序。它从不显示“作为 Scala 应用程序运行”,而是显示“作为 Java 应用程序运行”或“Java 小程序”

I opened "run configuration" and clicked on "Scala application" and my project name is "test" and second column is of "Class Main". What do I have to fill in? I filled in "Main.Scala", but it states "could not find mainmain class main.scala".

我打开“运行配置”并单击“Scala 应用程序”,我的项目名称是“test”,第二列是“Class Main”。我需要填写什么?我填写了“Main.Scala”,但它指出“找不到 mainmain 类 main.scala”。

Can you help me with running this project?

你能帮我运行这个项目吗?

回答by LMDDGTFY

If you want to run the whole project it should have a "main class", in any of your Scala objects you should be defining:

如果你想运行整个项目,它应该有一个“主类”,在你的任何 Scala 对象中你应该定义:

def main(args:Array[String]) { <some nice code here> }

From there it should be "calling" the rest of your objects to do whatever the whole project does and in the "Class Main" column you should specify the fully qualified name of your object. For instance, if you defined the main in a class called "Start" in the package "starter", in the "Class Main" field you should state "starter.Start".

从那里它应该“调用”其余的对象来执行整个项目所做的任何事情,并且在“Class Main”列中,您应该指定对象的完全限定名称。例如,如果您在包“starter”中的名为“Start”的类中定义了 main,则应在“Class Main”字段中声明“starter.Start”。

But on the other hand if you only want to run a Scala object it should extend App, if it doesn't extend App, Scala IDE won't add the "Run as Scala Application...":

但另一方面,如果您只想运行一个 Scala 对象,它应该扩展 App,如果它不扩展 App,Scala IDE 将不会添加“作为 Scala 应用程序运行...”:

package greeter
object Hello extends App {
  println("Hello, World!")
}

回答by Rajgopal C

Right click your project and check the "Scala Compiler" settings. Check the "Project Specific" checkbox and try checking if you can run your Scala object (which should extend App).

右键单击您的项目并检查“Scala 编译器”设置。选中“项目特定”复选框并尝试检查您是否可以运行您的 Scala 对象(它应该扩展 App)。

回答by Ram Janovski

make sure your declared packagein your source code matches the directory structureunder your source directory.

确保源代码中声明的与源目录下的目录结构匹配。

in this case, a sourcefile declaring package "greeter" will auto-run as scala if the source file is indeed under src/greeter/Hello.scala (and not just under src/Hello.scala)

在这种情况下,如果源文件确实在 src/greeter/Hello.scala 下(而不仅仅是在 src/Hello.scala 下),则声明包“greeter”的源文件将作为 scala 自动运行

Its a common mistake that doesn't get highlighted by the syntax checker.

这是语法检查器没有突出显示的常见错误。

回答by jopasserat

If you installed Scala plugin for Eclipse, open the Scala perspective. Then right-click on your project and select "Add Scala Nature" in "Configure" menu.

如果您为 Eclipse 安装了 Scala 插件,请打开 Scala 透视图。然后右键单击您的项目并在“配置”菜单中选择“添加 Scala Nature”。

You should now be able to run your Scala applications.

您现在应该能够运行您的 Scala 应用程序。

回答by jimakos17

Download from this link Scala IDE

从此链接下载Scala IDE

Restart Eclipse, create Scala Project, then create Scala Object and type this.

重新启动 Eclipse,创建 Scala 项目,然后创建 Scala 对象并键入它。

 package greeter

    object Hello {
        def main(args:Array[String]) { 
          println("Hello, World") 
        }   
    }

Run as Scala Application

作为 Scala 应用程序运行

回答by Serendipity

If it is the first timeyou run the Scala IDE for eclipse after setting it up and creating your project, all the thing you need is to just save your project and restart the IDE. At the next start, the "run as Scala Application" is appeared and can be used.

如果这是您在设置并创建项目后第一次为 Eclipse 运行 Scala IDE,您需要做的只是保存您的项目并重新启动 IDE。下次启动时,会出现“run as Scala Application”,可以使用了。

回答by Fabian Steeg

I had issues with the Scala IDE for Eclipse running Scala applications that extend Application, but running objects with a proper main method, i.e. def main(args:Array[String]) {/*...*/}always works fine for me.

我有 Scala IDE for Eclipse 运行扩展Application 的Scala 应用程序的问题,但使用适当的 main 方法运行对象,即def main(args:Array[String]) {/*...*/}对我来说总是很好。

回答by sparkDabbler

Right click on the Project --> Click on Run Configurations --> In the Run Configurations window select the "Scala Application"

右键单击项目 --> 单击运行配置 --> 在运行配置窗口中选择“Scala 应用程序”

Run Configurations Window

运行配置窗口

回答by nir

I was having similar issue. Make sure your java and scala files are not in the same package. I changed the package names and it worked for me

我有类似的问题。确保您的 java 和 scala 文件不在同一个包中。我更改了包名称,它对我有用

回答by Sandeep Das

Just a pointer .. I had faced same difficulty . Being experienced from JAVA , instead of creating a Spark object I was creating spark class that why I was not getting this option .

只是一个指针.. 我遇到了同样的困难。从 JAVA 获得经验,而不是创建 Spark 对象,我创建了 spark 类,这就是为什么我没有得到这个选项。

Hope my experience helps .

希望我的经验有帮助。