If you’re ever working on a really small Scala project — something that contains only a few source code files — and don’t want to use SBT to create a JAR file, you can do it yourself manually. Let’s look at a quick example. Note that the commands below work on Mac and Linux systems, and should work on Windows with minor changes.
Three assumptions in this process are:
Here’s a Unix shell script that I use to search Java Jar files for any type of string pattern. You can use it to search for the name of a class, the name of a package, or any other string/pattern that will show up if you manually ran
jar tvf on each jar file. The advantage of this script — if you’re a Unix, Linux, or Cygwin user — is that it will search through all jar files in the current directory:
If you want to run/execute a
main method from a jar file you created with Scala and the
sbt package command, this little tutorial shows how to do it. To make things a little more complicated, my Scala project depends on three external jar files, and the
main method requires a command-line argument.
As noted in the Summary, you’ll probably want to use a tool like SBT-Assembly for larger projects.
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 18.2, “How to compile, run, and package a Scala project with SBT.”Back to top
You want to use SBT to compile and run a Scala project, and package the project as a JAR file.Back to top
Create a directory layout to match what SBT expects, then run
sbt compile to compile your project,
sbt run to run your project, and
sbt package to package your project as a JAR file.
I just started using Android Studio 1.x and quickly ran into a problem where I needed to use a Jar file in my Android project. In short, this image shows the steps I followed to import the Jar file into my project. As an important note, I put the Jar file I needed in an app/libs folder, which I created in my project. As the image shows, this affects your Gradle build configuration. (I found this info at this SO link.)
Last week I ran into a situation where I was bundling a Java/Scala application on a Mac OS X system, which (as usual) requires you to bundle your code in a Jar file. I was working with the jnativehook library, where I saw some code like this:
I don’t know why, but without digging into it more, all I can say right now is that I can’t use the Java Sound API from within SBT. Whenever I try running
sbt run, I keep getting the following error message, even though I know that my app and sound file work fine when I package my Java application normally:
javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file
As part of the debugging process I created a little shell script named run.sh that contained these two lines: