Reputation: 99
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.
Upvotes: 9
Views: 15051
Reputation: 1
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.
Upvotes: 0
Reputation: 179
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
Upvotes: 1
Reputation: 12158
% 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
%
Upvotes: 0
Reputation: 9396
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.
Upvotes: 3
Reputation: 1184
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.
Create a Java class with a main() method which simply forwards the call to your Scala code.
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.
Now you are ready to dig out the runnable jar option from the Eclipse's menu: File -> Export -> Java -> Runnable JAR file
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.
The jar will be created in the Eclipse's workspace by default.
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.
Upvotes: 11