Scala: creating a small executable Jar using Scala external libraries

I am trying to pack a small application (while still learning Scala!) In a "clean way". The goal is to have an executable JAR file. I have done the following:

  • packaged JAR using sbt -> will work with
scala -cp myjarfile.jar MyClass 

or

 java -classpath path\to\scala-library.jar;myjarfile.jar MyClass 

but will not work with

 java -jar myjarfile.jar 

because then scala / ScalaObject could not be found. And do not use adding classpath for this last one, since the -jar parameter will ignore the -classpath parameter. Note that I added scala libs to my system CLASSPATH variable, but it seems to be ignored when -jar is used.

  • added the contents of the scala library (first unpacking them) to the jar created by sbt. Then it works (the jar can be double-clicked and launched), but the manipulation is somewhat long, and the resulting file has several megabytes. Not ideal.

After several hours of working on the Internet, I see no way to create a small jar file that will run when double-clicked and will not have painful manipulations. What am I missing? I suppose there must be some way to determine where the scala libraries are at the system level. How do you work with small applications that you want to compile and ready to work for better performance?

Note that although I use sbt, I don't have a tool for jar (I rely on the JRE, not the JDK).

Thanks! Pierric.

+8
scala jar executable-jar
source share
1 answer

The following setting works for me:

  • there is scala-library.jar in the same folder as the executable jar (and call java from there)
  • put this in your manifest: Class-Path: scala-library.jar

Another option is to combine the contents of scala -library.jar into your application jar. The disadvantage is that it will increase its size. But you can use Proguard to remove unused classes from your last jar. I think there is an easy way to use sbt to package an executable jar using proguard.

Regarding the reduction using Proguard, you can see this page . This is about Android development; just ignore this part and look at the tables of shrinking results. Some sample applications are reduced to less than 100 KB.

Edit

You may need to clarify your question a bit. What are you trying to achieve? Do you want to install the program only in your system or want to distribute it?

If you want to quickly launch a Java application without significantly influencing the startup time of the JVM, you can take a look at nailgun .

+3
source share

All Articles