eclipse 从 Scala 生成 .jar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6773180/
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-19 16:40:06  来源:igfitidea点击:

Generate .jar from scala

eclipsescalajar

提问by Snoopy

I'm developing an application on Eclipse with Scala and a would like to create a .jar. I have found tuto to do that, but it use the package scala.tools.nsc and I don't know where I can found this thing.
I have tried too, to generate the .class and then with the command jar cmf ....to generate the .jarbut when I launch the .jaran error occur. (NoClassFound)
With sbt I have tried too, but when I compile my project that work with eclipse a lot of error appear.

我正在使用 Scala 在 Eclipse 上开发一个应用程序,并且想创建一个 .jar。我发现 tuto 可以做到这一点,但它使用了 scala.tools.nsc 包,我不知道在哪里可以找到这个东西。
我也尝试过,生成 .class,然后使用命令jar cmf ....生成,.jar但是当我启动时.jar发生错误。(NoClassFound)
使用 sbt 我也尝试过,但是当我编译与 Eclipse 一起使用的项目时,会出现很多错误。

Somebody can me explain how I can simply create a .jar with Eclipse or another tools.

有人可以解释我如何使用 Eclipse 或其他工具简单地创建一个 .jar。

回答by Lex

Eclipse has a build-in option to generate runnable jars, but it is well hidden. Also it does not recognize Scala's main() signatures, so you will have to create a Java class with a main() method.

Eclipse 有一个内置选项来生成可运行的 jar,但它隐藏得很好。它也无法识别 Scala 的 main() 签名,因此您必须使用 main() 方法创建一个 Java 类。

  1. Create a Java class with a main() method which simply forwards the call to your Scala code.

  2. Then right click on your newly created Java class and select: Run As -> Java Application. This will create a runnable configuration which will later be used as a part of your runnable jar.

  3. Now you are ready to dig out the runnable jar option from the Eclipse's menu: File -> Export -> Java -> Runnable JAR file

  4. In the presented dialog select the Launch Configuration you have created earlier (in step2), name your jar (myapp.jar), select your dependency export options and click finish.

  5. The jar will be created in the Eclipse's workspace by default.

  6. Run the jar using the line: scala myapp.jar

  1. 使用 main() 方法创建一个 Java 类,该方法只是将调用转发到您的 Scala 代码。

  2. 然后右键单击您新创建的 Java 类并选择:Run As -> Java Application。这将创建一个可运行配置,稍后将用作可运行 jar 的一部分。

  3. 现在您已准备好从 Eclipse 的菜单中挖掘出可运行 jar 选项:文件 -> 导出 -> Java -> 可运行 JAR 文件

  4. 在出现的对话框中,选择您之前(在第 2 步中)创建的启动配置,命名您的 jar (myapp.jar),选择您的依赖项导出选项并单击完成。

  5. 默认情况下,jar 将在 Eclipse 的工作区中创建。

  6. 使用以下行运行 jar:scala myapp.jar

Your question about missing images: Eclipse requires a manual refresh when files are added or removed. Right click on your project and select Refresh.

您关于丢失图像的问题:添加或删除文件时,Eclipse 需要手动刷新。右键单击您的项目并选择刷新。

This concludes the tutorial on the highly intuitive Eclipse interface.

关于高度直观的 Eclipse 界面的教程到此结束。

Note: Instructions for Eclipse version 3.6.2.

注意:Eclipse 3.6.2 版的说明。

回答by ron

Jars

罐子

When you create a jar out of your classes, possibly the dependencies are not included in that jar. When running that jar, you need to put the jars containing the dependencies on the classpath (with the -cp switch to java). Most important dependency is the scala-libraryjar. Of course knowing what is not found by NoClassDefFound would help.

当您从类中创建 jar 时,可能依赖项不包含在该 jar 中。运行该 jar 时,您需要将包含依赖项的 jar 放在类路径上(使用 -cp 切换到 java)。最重要的依赖是scala-libraryjar。当然,知道 NoClassDefFound 没有找到什么会有所帮助。

Sbt

Sbt

When building with sbt, maybe it is missing dependencies that you have manually added to the Eclipse project? (Note: I did not use sbt).

使用 sbt 构建时,是否缺少您手动添加到 Eclipse 项目中的依赖项?(注意:我没有使用 sbt)。

Maven

马文

I found the clearest and most painless way is to go with maven alone, or possibly maven + Intellij Idea (community edition is free) + Scala Plugin. Works very smooth.

我发现最清晰、最轻松的方法是单独使用 maven,或者可能使用 maven + Intellij Idea(社区版免费)+ Scala 插件。工作非常流畅。

For maven, you need to adapt the available scala archetype a bit since the libraries it refers to are not the most recent version, but apart from that it is very fine.

对于 maven,您需要稍微调整可用的 scala 原型,因为它引用的库不是最新版本,但除此之外它非常好。

Here is a pom.xmlI'm using: https://gist.github.com/1096870

这是pom.xml我正在使用的:https: //gist.github.com/1096870

Use the standard maven folder structure (source root is src/main/scala) and then mvn packagecreates the jar fine.

使用标准的 maven 文件夹结构(源根目录为 src/main/scala),然后mvn package创建 jar。

回答by Kumar Basapuram

Use the below steps for time being .But this is not the full time solution.Better to go for sbt build jar for Spark-Scala combination. Temporary solution using java class ,calling the scala main class. MainClass.java

暂时使用以下步骤。但这不是全职解决方案。最好为 Spark-Scala 组合使用 sbt build jar。使用java类的临时解决方案,调用scala主类。 主类.java

public class MainClass {
    public static void main(String[] args) {
        SampleApp app=new SampleApp();
        app.main(args); }
}

SampleApp.scala

示例应用程序.scala

class SampleApp {
   def main(args: Array[String]) {  
     println("First Scala SampleApp")}  
}

Export it as a jar by using normal java jar export by choosing the MainClass main method. name the jar file as Sample.jar Run it in the Cluster using below command.

通过选择 MainClass main 方法,使用普通的 java jar 导出将其导出为 jar。将 jar 文件命名为 Sample.jar 使用以下命令在集群中运行它。

/spark/bin/spark-submit --class com.scala.MainClass SampleScala.jar

/spark/bin/spark-submit --class com.scala.MainClass SampleScala.jar

The output you can get as: First Scala SampleApp

您可以获得的输出为:First Scala SampleApp

回答by Sean

To build off of what @Kumar Basapuram wrote:

以@Kumar Basapuram 所写的内容为基础:

Make a java class called "Wrapper.java".

创建一个名为“Wrapper.java”的 java 类。

package animals;
public class Wrapper {
    public static void main(String[] args) {
        SampleApp app=new SampleApp();
        app.main(args);
    }
}

Link this main method to the main method in the "SampleApp.scala" class.

将此主要方法链接到“SampleApp.scala”类中的主要方法。

package animals
class SampleApp {
  def main(args: Array[String]){
     var c = new Cow("Bessie", 100)
     println(c.speak)
     var h = new Horse("CJ", 50)
     println(h.speak)
     var s = new Sheep("Little Lamb", 25)
     println(s.speak)

     println(s.weigh)
     println(h.weigh)
     println(c.weigh)
  }
}

Project with Java and Scala Classes Picture

带有 Java 和 Scala 类的项目图片

Right Click on the Project ScalaPracticeCreation. Click Export... Click Runnable JAR file under the Java folder Exporting Scala Class into a jar Executable Picture

右键单击 ScalaPracticeCreation 项目。点击Export... 点击Java文件夹下的Runnable JAR file Exporting Scala Class into a jar Executable Picture

Click Next >

单击下一步 >

Select Wrapper - ScalaPracticeCreations

选择包装器 - ScalaPracticeCreations

Select Export destination to a place on your computer

选择将目标导出到您计算机上的某个位置

Select "Extract required libraries into generated JAR" under the "Library handling:" option

在“库处理:”选项下选择“将所需的库提取到生成的 JAR 中”

Click Finish

单击完成

Run the file through the Eclipse IDE and it works.

通过 Eclipse IDE 运行该文件,它可以工作。

Run it through the Command Prompt and it does not work. Command Prompt Picture

通过命令提示符运行它,它不起作用。 命令提示符图片

To fix this remove the println methods from the "SampleApp.scala".

要解决此问题,请从“SampleApp.scala”中删除 println 方法。

package animals
class SampleApp {
  def main(args: Array[String]) {
    var c = new Cow("Bessie", 100)
    var h = new Horse("CJ", 50)
    var s = new Sheep("Little Lamb", 25)
    c.weigh().toString()
  }
}

add "System.out.println(app.main(args));" to replace "app.main(args);" in the Wrapper.java class

添加“System.out.println(app.main(args));” 替换“app.main(args);” 在 Wrapper.java 类中

package animals;
public class Wrapper {
    public static void main(String[] args) {
        SampleApp app=new SampleApp();
        System.out.println(app.main(args));
    }
}

Now reexport the program after running it. success in the command prompt Picture

现在在运行后重新导出该程序。 在命令提示符下成功图片

Now it works.

现在它起作用了。

Here are the extra filler .scala classes. Note that the Demo.scala class is irrelevant.

这是额外的填充 .scala 类。请注意,Demo.scala 类是无关紧要的。

Weight.scala:

重量.scala:

package animals
abstract class Weight(size: Int) {
  def weigh = "My size is " + size
}

Animal.scala:

动物.scala:

package animals
abstract class Animal(name: String, weight: Int) extends Weight(weight){
  def speak = name + " says " + sound
  def sound: String
  override def weigh() = "My name is " + name + " and I weigh: " + weight
}

Cow.scala:

牛.scala:

package animals
class Cow (name: String, weight: Int) extends Animal(name,weight){
  override def sound() = "mooooo"
}

Horse.scala:

马.scala:

package animals
class Horse (name: String, weight: Int) extends Animal(name,weight){
  override def sound() = "neigh"
}

Sheep.scala:

羊.scala:

package animals    
class Sheep (name: String, weight: Int) extends Animal(name,weight) {
  override def sound() = "baaaa"
}

Note that this may not be the best solution although it is a functional solution. Scala sbt may be a better solution: Scala sbtor this Scala sbt-assembly.

请注意,这可能不是最佳解决方案,尽管它是一个功能解决方案。Scala sbt 可能是更好的解决方案:Scala sbt或这个Scala sbt-assembly

回答by psp

% cat a.scala 
package foo

object Whee {
  def main(args: Array[String]): Unit = {
    println("I'm in a jar")    
  }
}
% scalac29 -d whee.jar a.scala 
% scala29 -cp whee.jar foo.Whee
I'm in a jar
%