Building a jar and running

15 views
Skip to first unread message

Nicholas James

unread,
Jul 29, 2020, 7:27:38 PM7/29/20
to ScalaPB
Hey I'm having some issue using the jar that is create when I run "sbt package". I'm able to run my code successfully when I use "sbt run" for the project root but not when trying to use the jar, which I find odd.

My project has the typical structure and includes my proto files under the src/main/protobuf directory. And my build.sbt is as follows.

name := "ProfileGenerator"
version := "1.0.0"
scalaVersion := "2.13.3"

PB.targets in Compile := Seq(
  scalapb.gen() -> (sourceManaged in Compile).value / "scalapb"
)

// (optional) If you need scalapb/scalapb.proto or anything from
// google/protobuf/*.proto
libraryDependencies += "com.thesamet.scalapb" %% "scalapb-runtime" % scalapb.compiler.Version.scalapbVersion % "protobuf"

After running "sbt package" I see the target/scala-2.13/profilegenerator_2.13-1.0.0.jar as expected. However, when I run "scala profilegenerator_2.13-1.0.0.jar" I get a java.lang.ClassNotFoundException.

java.lang.ClassNotFoundException: scalapb.GeneratedMessageCompanion
    at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:435)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    at java.base/java.lang.ClassLoader.defineClass1(Native Method)
    at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1017)
    at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:151)
    at java.base/java.net.URLClassLoader.defineClass(URLClassLoader.java:514)
    at java.base/java.net.URLClassLoader$1.run(URLClassLoader.java:422)
    at java.base/java.net.URLClassLoader$1.run(URLClassLoader.java:416)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:691)
    at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:415)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    at ccda.ProfileCreator.getProfileKvFromFile(ProfileCreator.scala:37)
    at ccda.ProfileCreator.$anonfun$create$1(ProfileCreator.scala:26)
    at scala.collection.StrictOptimizedIterableOps.map(StrictOptimizedIterableOps.scala:99)
    at scala.collection.StrictOptimizedIterableOps.map$(StrictOptimizedIterableOps.scala:86)
    at scala.collection.mutable.ArrayBuffer.map(ArrayBuffer.scala:40)
    at ccda.ProfileCreator.create(ProfileCreator.scala:26)
    at Main$.delayedEndpoint$Main$1(Main.scala:11)
    at Main$delayedInit$body.apply(Main.scala:5)
    at scala.Function0.apply$mcV$sp(Function0.scala:39)
    at scala.Function0.apply$mcV$sp$(Function0.scala:39)
    at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:17)
    at scala.App.$anonfun$main$1(App.scala:73)
    at scala.App.$anonfun$main$1$adapted(App.scala:73)
    at scala.collection.IterableOnceOps.foreach(IterableOnce.scala:553)
    at scala.collection.IterableOnceOps.foreach$(IterableOnce.scala:551)
    at scala.collection.AbstractIterable.foreach(Iterable.scala:920)
    at scala.App.main(App.scala:73)
    at scala.App.main$(App.scala:71)
    at Main$.main(Main.scala:5)
    at Main.main(Main.scala)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at scala.reflect.internal.util.ScalaClassLoader.$anonfun$run$2(ScalaClassLoader.scala:105)
    at scala.reflect.internal.util.ScalaClassLoader.asContext(ScalaClassLoader.scala:40)
    at scala.reflect.internal.util.ScalaClassLoader.asContext$(ScalaClassLoader.scala:37)
    at scala.reflect.internal.util.ScalaClassLoader$URLClassLoader.asContext(ScalaClassLoader.scala:130)
    at scala.reflect.internal.util.ScalaClassLoader.run(ScalaClassLoader.scala:105)
    at scala.reflect.internal.util.ScalaClassLoader.run$(ScalaClassLoader.scala:97)
    at scala.reflect.internal.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:130)
    at scala.tools.nsc.CommonRunner.run(ObjectRunner.scala:29)
    at scala.tools.nsc.CommonRunner.run$(ObjectRunner.scala:28)
    at scala.tools.nsc.JarRunner$.run(MainGenericRunner.scala:17)
    at scala.tools.nsc.CommonRunner.runAndCatch(ObjectRunner.scala:35)
    at scala.tools.nsc.CommonRunner.runAndCatch$(ObjectRunner.scala:34)
    at scala.tools.nsc.JarRunner$.runJar(MainGenericRunner.scala:17)
    at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:76)
    at scala.tools.nsc.MainGenericRunner.run$1(MainGenericRunner.scala:91)
    at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:103)
    at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:108)
    at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)

Nadav Samet

unread,
Jul 29, 2020, 8:34:49 PM7/29/20
to ScalaPB
Hi Nicholas,

"sbt package" does not pack the dependencies of your project into the jar. Therefore, when you invoke it using the scala command-line utility, you need to pass not only the project jar but the rest of the jars that need to be on the classpath using the "-cp" option. See this stackoverflow question: https://stackoverflow.com/questions/24238060/how-to-run-jar-generated-by-package-possibly-with-other-jars-under-lib

To create something that is easy to run from the command line, look into:
Hope this helps!

Nadav

Nadav Samet

unread,
Jul 29, 2020, 8:37:50 PM7/29/20
to ScalaPB
To further clarify, the dependency that is missing here is scalapb-runtime - which you can downloan (or find locally somewhere under the targets directory), and provide it through the "-cp" flag to scala or java CLI.

Nicholas James

unread,
Jul 30, 2020, 12:24:49 AM7/30/20
to ScalaPB
Thanks for the information, this helped me out a ton.
Reply all
Reply to author
Forward
0 new messages