简体   繁体   English

java可以运行编译好的scala代码吗?

[英]Can java run a compiled scala code?

Can command java run a compiled scala code?命令java可以运行已编译的 Scala 代码吗? If so, why do we have an exclusive command scala ?如果是这样,为什么我们有一个独占命令scala

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.如果在类路径中包含 Scala 的所有必要运行时库(scala-library.jar、scala-swing.jar ...),则可以运行由 Scala 生成的字节码。 The scala command does this automatically, and supports Scala specific command line arguments. scala 命令会自动执行此操作,并支持 Scala 特定的命令行参数。

Yes, it can.是的,它可以。 Scala is compiled down to Java bytecode. Scala 被编译成 Java 字节码。 But remember that it depends on the Scala runtime classes, so you need to still have Scala's jar files on the classpath.但是请记住,它依赖于 Scala 运行时类,因此您仍然需要在类路径上保留 Scala 的 jar 文件。

If so, why do we have an exclusive command scala?如果是这样,为什么我们有一个独占命令 scala?

Convenience wrapper.方便包装。

Scala is designed to integrate easily with applications that run on modern virtual machines, primarily the Java virtual machine (JVM). Scala 旨在与在现代虚拟机(主要是 Java 虚拟机 (JVM))上运行的应用程序轻松集成。 The main Scala compiler, scalac, generates Java class files that can be run on the JVM.主要的 Scala 编译器 scalac 生成可以在 JVM 上运行的 Java 类文件。 -> http://www.artima.com/scalazine/articles/steps.html -> http://www.artima.com/scalazine/articles/steps.html

As long as you have installed the scala runtime you should be fine: compile classes with scalac and run them with java .只要您安装了 scala 运行时,您就应该没问题:使用scalac编译类并使用java运行它们。

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,如果不带参数运行,将运行交互式 shell

scala, if run with a text file name as parameter, will regard the file as a scala script scala,如果以文本文件名作为参数运行,则会将该文件视为scala脚本

those two can't be done using java这两个不能用java来完成

If you look closely, the scala command is simply a bash helper-script which summarize to the below command:如果仔细观察, scala命令只是一个 bash 帮助程序脚本,它总结为以下命令:

$cat /usr/local/Cellar/scala@2.11/2.11.12_1/libexec/bin/scala

execCommand \
  "${JAVACMD:=java}" \
  $JAVA_OPTS \
  "${java_args[@]}" \
  "${classpath_args[@]}" \
  -Dscala.home="$SCALA_HOME" \
  $OVERRIDE_USEJAVACP \
  "$EMACS_OPT" \
  $WINDOWS_OPT \
   scala.tools.nsc.MainGenericRunner  "$@"

There are 2 things required to run a .class file compiled using scalac ( the scala compiler) using the java command.使用java命令运行使用scalac (scala 编译器)编译的.class文件需要两件事。

  1. We need to include the scala-library.jar and the location of the .class file in the classpath .我们需要在classpath包含scala-library.jarlocation of the .class filelocation of the .class file To find the location of scala-library.jar , please execute the below:要找到scala-library.jar的位置,请执行以下命令:

    which scala /usr/local/opt/scala@2.11/bin/scala

    In my case the scala-*.jar files are in : /usr/local/Cellar/scala@2.11/2.11.12_1/idea/lib on Mac就我而言, scala-*.jar文件位于:Mac 上的/usr/local/Cellar/scala@2.11/2.11.12_1/idea/lib

  2. The location of the Main2.class file which is in /training/example1/scala . Main2.class文件在/training/example1/scala

So, to execute the program we could use the below command:因此,要执行该程序,我们可以使用以下命令:

java -cp /usr/local/Cellar/scala@2.11/2.11.12_1/idea/lib/scala-library.jar:/training/example1/scala/ Main2

EDIT-1: If you are using windows, please use semicolon(;) as the separator in java classpath command. EDIT-1:如果您使用的是windows,请在java classpath 命令中使用分号(;) 作为分隔符。

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

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