Generate .jar from scala

Viewed 14461

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.

5 Answers

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.

Related