Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a jar file from a Scala file

Tags:

java

jar

scala

I'm new to Scala and don't know Java. I want to create a jar file out of a simple Scala file. So I have my HelloWorld.scala, generate a HelloWorld.jar.

Manifest.mf:

Main-Class: HelloWorld 

In the console I run:

fsc HelloWorld.scala jar -cvfm HelloWorld.jar Manifest.mf HelloWorld\$.class HelloWorld.class java -jar HelloWorld.jar    => "Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld/jar"  java -cp HelloWorld.jar HelloWorld    => Exception in thread "main" java.lang.NoClassDefFoundError: scala/ScalaObject     at java.lang.ClassLoader.defineClass1(Native Method)     at java.lang.ClassLoader.defineClass(ClassLoader.java:675)     at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)     at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)     at java.net.URLClassLoader.access$100(URLClassLoader.java:56)     at java.net.URLClassLoader$1.run(URLClassLoader.java:195)     at java.security.AccessController.doPrivileged(Native Method)     at java.net.URLClassLoader.findClass(URLClassLoader.java:188)     at java.lang.ClassLoader.loadClass(ClassLoader.java:316)     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:280)     at java.lang.ClassLoader.loadClass(ClassLoader.java:251)     at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:374)     at hoppity.main(HelloWorld.scala) 
like image 269
nuriaion Avatar asked Apr 30 '09 21:04

nuriaion


People also ask

How do I create a jar in Scala code?

To build a jar file with your application in case if you have no external dependencies, you can run sbt package and it will build a hello-world_2. 11_1. 0. jar file with your code so you can run it with java -jar hello-world.

What is a JAR file in Scala?

jar file created by SBT is executable by scala interpreter or scala commands but not by the java command(i.e java -jar name-of-file. jar). The reasion behind this , jar created by 'sbt package' have dependency on scala class files which are not included in the file.


2 Answers

Sample directory structure:

X:\scala\bin X:\scala\build.bat X:\scala\MANIFEST.MF X:\scala\src X:\scala\src\foo X:\scala\src\foo\HelloWorld.scala 

HelloWorld.scala:

//file: foo/HelloWorld.scala package foo {   object HelloWorld {     def main(args: Array[String]) {       println("Hello, world!")     }   } } 

MANIFEST.MF:

Main-Class: foo.HelloWorld Class-Path: scala-library.jar 

build.bat:

@ECHO OFF  IF EXIST hellow.jar DEL hellow.jar IF NOT EXIST scala-library.jar COPY %SCALA_HOME%\lib\scala-library.jar .  CALL scalac -sourcepath src -d bin src\foo\HelloWorld.scala  CD bin jar -cfm ..\hellow.jar ..\MANIFEST.MF *.* CD ..  java -jar hellow.jar 

In order to successfully use the -jar switch, you need two entries in the META-INF/MANIFEST.MF file: the main class; relative URLs to any dependencies. The documentation notes:

-jar

Execute a program encapsulated in a JAR file. The first argument is the name of a JAR file instead of a startup class name. In order for this option to work, the manifest of the JAR file must contain a line of the form Main-Class: classname. Here, classname identifies the class having the public static void main(String[] args) method that serves as your application's starting point. See the Jar tool reference page and the Jar trail of the Java Tutorial for information about working with Jar files and Jar-file manifests.

When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.

  • java command line usage
  • manifest spec

(Notes: JAR files can be inspected with most ZIP applications; I probably neglect handling spaces in directory names in the batch script; Scala code runner version 2.7.4.final .)


For completeness, an equivalent bash script:

#!/bin/bash  if [ ! $SCALA_HOME ] then     echo ERROR: set a SCALA_HOME environment variable     exit fi  if [ ! -f scala-library.jar ] then     cp $SCALA_HOME/lib/scala-library.jar . fi  scalac -sourcepath src -d bin src/foo/HelloWorld.scala  cd bin jar -cfm ../hellow.jar ../MANIFEST.MF * cd ..  java -jar hellow.jar 
like image 187
McDowell Avatar answered Oct 06 '22 16:10

McDowell


Because Scala scripts require the Scala libraries to be installed, you will have to include the Scala runtime along with your JAR.

There are many strategies for doing this, such as jar jar, but ultimately the issue you're seeing is that the Java process you've started can't find the Scala JARs.

For a simple stand-alone script, I'd recommend using jar jar, otherwise you should start looking at a dependency management tool, or require users to install Scala in the JDK.

like image 38
Matt Reynolds Avatar answered Oct 06 '22 15:10

Matt Reynolds