score:0
% 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
%
score:0
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.
score:1
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
score:3
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.
score:11
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.
Source: stackoverflow.com
Related Query
- Generate a JAR from one Scala source file
- Creating a jar file from a Scala file
- How to make a jar file from scala
- Is there any suitable way to generate a [jpg, png etc.] syntax diagram(and/or AST) directly from Scala Parser Combinators?
- Scala generate unique pairs from list
- Using SparkR JVM to call methods from a Scala jar file
- Lexer/parser to generate Scala code from BNF grammar
- Generate .jar from scala
- Are there any ways to generate Scala code from Protobuf files in a Maven build?
- Extract scala source code from jar
- Loading files from JAR in Scala
- Generate Scala code from Antlr
- Scala one-liner to generate MD5 Hash from string
- Can I generate Scala code from a template (of sorts)?
- Is it possible to generate Apply from WeakTypeTag inside a scala macro?
- How can I create a jar from some Scala source code?
- Problem creating an executable jar from scala file
- Attempting to generate types from other types in Scala
- How o run a NetBeans-built Scala application jar from command line outside IDE?
- How to make classes from a Scala jar library of mine accessible in Scala console and Scala scripts?
- Need to load a scala class at runtime from a jar and initialize it
- Using SBT to generate a JAR file name that doesn't include the Scala patch version
- Exporting Scala project as jar from Eclipse
- Generate combinations using items from different sets in Scala
- Cannot run jar file created from Scala file
- Running multiple Scala apps from one jar on JVM
- What would be a good approach to generate native code from an interpreter written with scala parser combinators?
- Generate Scala Source from Twirl Template
- using scala jar from java project in NetBeans
- generate case classes from CSV in Scala
More Query from same tag
- Making the type signature independent of a specific monad transformer stack (Scala)
- Generate random number with no side effect
- Problems Importing Scala Libraries In Intellij
- Writing ScalaTest for ReactiveMongoApi in Playframework 2.4?
- Publish RPM to remote YUM repository with SBT
- Write dataframe as csv to S3 with kms encrypted keys without providing key
- Significance of nullable in Spark dataframe StructField
- Retrieve the struct from array of structs when struct field of struct type matches with specific value in spark scala
- How to get all the children and sub-children of XML file using Scala
- Weird behavior accessing tuple from Java
- How to read response as Observable[String] with sttp
- What went wrong?
- is there a scalaTest mechanism similar to TestNg dependsOnMethods annotation
- How to import Play framework as a dependency in Scala project
- Scala Spark map by pairs of RDD elements
- How can I update a Swing canvas at a certain framerate?
- Let IntelliJ IDEA 12 monitor file changes
- Type-safe Builder: How to set compiler error message
- Why does my plugin have the Scala version in its folder structure?
- Scalatra with HTTPS?
- What is a way to implement a scala `Either[A,B]` structure in Koa / Javascript
- Read json key value but ignore object
- Return tail if list is not empty or return empty list
- How to count frequency of columns in row in a typedpipe in scalding?
- Importing MySQL JDBC driver in lift
- How to serialize more than 22 fields with nested object in spray?
- How to compare a string with another where the one has space in between
- Downloading source using Maven
- Iterating immutable Seq, Vector and List: any differences?
- How to return early in a pattern match of akka actor receive