scala java可以运行编译好的scala代码吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3626002/
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
Can java run a compiled scala code?
提问by nanda
Can command javarun a compiled scala code? If so, why do we have an exclusive command scala?
命令可以java运行已编译的 Scala 代码吗?如果是这样,为什么我们有一个独占命令scala?
回答by Landei
You can run byte code generated by Scala if you include all necessary runtime libs for Scala (scala-library.jar, scala-swing.jar ...) in the classpath. The scala command does this automatically, and supports Scala specific command line arguments.
如果在类路径中包含 Scala 的所有必要运行时库(scala-library.jar、scala-swing.jar ...),则可以运行由 Scala 生成的字节码。scala 命令会自动执行此操作,并支持 Scala 特定的命令行参数。
回答by Thilo
Yes, it can. Scala is compiled down to Java bytecode. But remember that it depends on the Scala runtime classes, so you need to still have Scala's jar files on the classpath.
是的,它可以。Scala 被编译成 Java 字节码。但请记住,它依赖于 Scala 运行时类,因此您仍然需要在类路径上保留 Scala 的 jar 文件。
If so, why do we have an exclusive command scala?
如果是这样,为什么我们有一个独占命令 scala?
Convenience wrapper.
方便包装。
回答by atamanroman
Scala is designed to integrate easily with applications that run on modern virtual machines, primarily the Java virtual machine (JVM). The main Scala compiler, scalac, generates Java class files that can be run on the JVM. -> http://www.artima.com/scalazine/articles/steps.html
Scala 旨在与在现代虚拟机(主要是 Java 虚拟机 (JVM))上运行的应用程序轻松集成。主要的 Scala 编译器 scalac 生成可以在 JVM 上运行的 Java 类文件。-> http://www.artima.com/scalazine/articles/steps.html
As long as you have installed the scala runtime you should be fine: compile classes with scalacand run them with java.
只要您安装了 Scala 运行时,您就应该没问题:scalac使用java.
回答by nanda
Just want to add my own answer as additional value for the future readers:
只想添加我自己的答案作为未来读者的附加价值:
scala, if run without parameter, will run an interactive shell
scala, if run with a text file name as parameter, will regard the file as a scala script
scala,如果不带参数运行,将运行交互式 shell
scala,如果以文本文件名作为参数运行,则会将该文件视为scala脚本
those two can't be done using java
这两个不能用java来完成

