jav*_*dba 30 scala intellij-idea
这是一个简单的scala脚本:
object test {
def hi() { print("hi there from here") }
}
test.hi()
Run Code Online (Sandbox Code Playgroud)
从命令行它执行预期:
scala /shared/scaladem/src/main/scala/test.scala
Loading /shared/scaladem/src/main/scala/test.scala...
defined module test
hi there from here
Welcome to Scala version 2.10.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_65).
Type in expressions to have them evaluated.
Type :help for more information.
scala>
Run Code Online (Sandbox Code Playgroud)
但是在Intellij中它会产生编译错误.右键单击| 运行test.scala
expected class or object definition
test.hi()
^
Run Code Online (Sandbox Code Playgroud)
BTW我也尝试过运行scala工作表.这是更糟糕的 - 大量的垃圾输出,甚至没有接近编译.
更新:似乎有一个较旧但相似的问题:
我进入了运行配置并按照指示取消选中"Make"(这很麻烦,但也是如此......)
但是,在进行更改后,我得到了一个不同的错误:
Exception in thread "main" java.lang.NoClassDefFoundError: scala/Either
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:113)
Caused by: java.lang.ClassNotFoundException: scala.Either
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:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 3 more
Run Code Online (Sandbox Code Playgroud)
注意:Scala-Library已正确设置:
另一个更新(在@ lhuang的评论之后) 我按照建议从头开始创建另一个项目.在这种情况下,Scala工作表正常工作(test.sc).但是scala脚本(通过"scala test.scala"运行命令行时仍然有效)仍然无效,即使在这个全新的"scala"项目中也是如此.
jav*_*dba 19
这里的答案是项目的组合:
这显示了Intellij及其scala插件的粗糙边缘.特别是当我想将scala与java集成时,如果甚至可能在此时使用Intellij显然很困难(需要频繁创建新的Scala项目对于大多数试图合并scala的java项目来说都是非首发).
但对于scala-first项目来说,似乎这可能是可行的.
Kai*_*hel 19
对我有用的是:
现在您可以运行脚本了.这有点不方便,但它确实有效.
注意:这适用于IntelliJ IDEA 14.x
小智 5
您的代码在命令行中工作,因为它是“脚本”,当您想让它在项目中运行时,有多种方法可以做到这一点:
通过创建扩展App的对象
object test {
def hi() { print("hi there from here") }
}
object runnable extends App {
test.hi()
}
Run Code Online (Sandbox Code Playgroud)或类似java的解决方案,即创建main方法
object test {
def hi() { print("hi there from here") }
}
object runnable {
def main(args: Array[String]) {
test.hi()
}
}
Run Code Online (Sandbox Code Playgroud)当需要作为脚本执行时 - 我这样做:选择要通过鼠标执行的代码,然后从上下文菜单中选择“将选择发送到 Scala 控制台”
归档时间: |
|
查看次数: |
21831 次 |
最近记录: |