0

I am working on a Scala project on IntelliJ with SBT as my build tool. I started working sbt build recently. This is my project structure: enter image description here

This is my build.sbt file:

name := "AnalyzeTables"
version := "0.1"    
scalaVersion := "2.11.8"

// https://mvnrepository.com/artifact/org.postgresql/postgresql
libraryDependencies += "org.postgresql" % "postgresql" % "42.1.4"

// https://mvnrepository.com/artifact/commons-codec/commons-codec
libraryDependencies += "commons-codec" % "commons-codec" % "1.13"

// https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
libraryDependencies += "org.apache.commons" % "commons-lang3" % "3.8.1"

// https://mvnrepository.com/artifact/log4j/log4j
//libraryDependencies += "log4j" % "log4j" % "1.2.17"

I have Class.forName("org.postgresql.Driver) in my code to connect to database and query. Along with that, I have password decryption & logger added in the code. I am running the jar in the below format:

scala <jarname> <argument I use in my code>

The problem here is if I just submit in the way I mentioned above, I see ClassNotFoundException for postgres driver. So I add it to the classpath of the jar and submit it as below.

scala -cp /path/postgresql-42.1.4.jar <jarname> <argument I use in my code>

Now I get exception for Logger. So I add it to classpath again and it becomes:

scala -cp /path/postgresql-42.1.4.jar:/path/log4j-1.2.17.jar <jarname> <argument I use in my code>

Now I get exception for commons-codec, so I added that as well. scala -cp /path/postgresql-42.1.4.jar:/path/log4j-1.2.17.jar:/path/commons-codec-1.13.jar

Now that jar is running fine and I can see the result. So I have the dependencies added to the build.sbt file. I also did the below operation:

project structure -> Modules -> Dependencies -> + -> jars -> Add all the missing jars that are giving problems

If I remove all the -cp parameters and submit the jar with just: scala <jarname> <argument> it goes back again to ClassNotFoundException to postgres jar. So what is the point of adding dependencies on build.sbt file and then add them in the classpath again ? Is there ay setting I am missing or am I looking it in the wrong way ?

Edit: After suggestions I created a new project & copied all the code into it and added the plugin addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.5") in a new file plugins.sbt file which I created in the dir /project/ as shown in the image below. enter image description here

I can see the plugin in the sbt-plugins dir. But when I build the jar once again and export, it still shows 11kb jar instead of a fat jar.

Metadata
  • 2,127
  • 9
  • 56
  • 127
  • 3
    I believe you need [**sbt-assemby**](https://github.com/sbt/sbt-assembly). – Luis Miguel Mejía Suárez Feb 14 '20 at 12:59
  • 4
    Or coursier. https://get-coursier.io/ – Thilo Feb 14 '20 at 13:00
  • 1
    If you just want to run your code yourself (and do not need to ship a runnable jar to someone), you can also run your project from inside the sbt shell or from inside IntelliJ. These tools manage the runtime classpath for you as well. – Thilo Feb 14 '20 at 13:04
  • @Thilo I understand what you are saying but I need to deploy the jar file in my server. Hence trying to create a jar and work on it. – Metadata Feb 14 '20 at 13:09
  • 2
    Does this answer your question? [How to build an Uber JAR (Fat JAR) using SBT within IntelliJ IDEA?](https://stackoverflow.com/questions/28459333/how-to-build-an-uber-jar-fat-jar-using-sbt-within-intellij-idea) – Thilo Feb 14 '20 at 13:23
  • I have updated the question. I added the plugin and built the jar but still don't see the fat jar being built instead it is the same jar and classnotfound exceptions. Did I miss any steps ? – Metadata Feb 14 '20 at 15:20
  • Is there a sbt assembly command just like in Maven to create a fat jar ? – Metadata Feb 14 '20 at 15:21
  • 1
    you need to run the `assembly` task in SBT to build a fat jar with sbt-assembly – cbley Feb 14 '20 at 15:58

0 Answers0