从 scala 生成 .jar

发布于 2024-11-25 12:26:40 字数 313 浏览 1 评论 0原文

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

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

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 .jar but when I launch the .jar an error occur. (NoClassFound)
With sbt I have tried too, but when I compile my project that work with eclipse a lot of error appear.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

李白 2024-12-02 12:26:40

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

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

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

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

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

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

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

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

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

注意:Eclipse 版本 3.6.2 的说明。

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.

  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

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

This concludes the tutorial on the highly intuitive Eclipse interface.

Note: Instructions for Eclipse version 3.6.2.

暮倦 2024-12-02 12:26:40

Jars

当您从类中创建 jar 时,依赖项可能不包含在该 jar 中。运行该 jar 时,您需要将包含依赖项的 jar 放在类路径上(使用 -cp 开关切换到 java)。最重要的依赖项是 scala-library jar。当然,了解 NoClassDefFound 未找到的内容会有所帮助。

Sbt

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

Maven

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

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

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

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

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-library jar. Of course knowing what is not found by NoClassDefFound would help.

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).

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.

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.

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

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

探春 2024-12-02 12:26:40

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

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

SampleApp.scala

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

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

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

您可以获得的输出为:
第一个 Scala 示例应用程序

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

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

SampleApp.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.

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

The output you can get as:
First Scala SampleApp

梦过后 2024-12-02 12:26:40
% 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
%
% 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
%
绝不服输 2024-12-02 12:26:40

要构建@Kumar Basapuram 所写的内容:

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

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

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

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)
  }
}

包含 Java 和 Scala 类图片的项目

右键单击​​项目 ScalaPracticeCreation。
单击导出...
单击 Java 文件夹下的 Runnable JAR 文件
将 Scala 类导出到 jar 可执行图片

点击下一步 >

选择 Wrapper - ScalaPracticeCreations

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

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

单击“完成”

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

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

要解决此问题,请从“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()
  }
}

添加“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));
    }
}

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

现在可以了。

以下是额外的填充 .scala 类。请注意,Demo.scala 类是不相关的。

Weight.scala:

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

Animal.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:

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

Horse.scala:

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

Sheep.scala:

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

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

To build off of what @Kumar Basapuram wrote:

Make a java class called "Wrapper.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.

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

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

Click Next >

Select Wrapper - ScalaPracticeCreations

Select Export destination to a place on your computer

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

Click Finish

Run the file through the Eclipse IDE and it works.

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".

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

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.

Weight.scala:

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

Animal.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:

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

Horse.scala:

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

Sheep.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 sbt or this Scala sbt-assembly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文