Sbt run question

Do you know why “sbt run” can success, but running “scala program.scala” directly got the error following?

$ scala target/scala-2.13/hivedb_2.13-0.1.jar 
java.lang.ClassNotFoundException: org.apache.hive.jdbc.HiveDriver
	at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:476)
	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.Class.forName0(Native Method)
	at java.base/java.lang.Class.forName(Class.java:315)

sbt run can be fine:

$ sbt run
[success] Total time: 2 s, completed Mar 1, 2022, 11:30:43 AM

This is a simple Hive client using the only package:

libraryDependencies += "org.apache.hive" % "hive-jdbc" % "2.3.9"

Thank you.

You would need to run scala with a -classpath argument with the paths of all the dependency JARs, including any transitive dependencies.

In sbt, export dependencyClasspath will show you what that looks like. (Note that the output will include Scala library JARs which you don’t need to include when running the code with the scala command, but do need to include if you’re running it with the java command.)

4 Likes

may I ask for the complicated java dependencies how do you deploy your program?
It has no possible to ask every server box to install sbt and run scala program via sbt run.

Thanks.

As I mentioned here there are a couple of options, it depends on your use case to determine which is the best one.

3 Likes
  • for script app I think the raw file can be possible to deploy
  • for spark app here I always made them work correctly
  • for hadoop and logs(logstash, flume) etc, there are many dependencies issues.
  • for web app, can I run it under a container like Resin?

thanks again.

Yes, but the target machine needs a JDK and Scala or maybe Ammonite.

What kind of issues?
If missing dependencies, you need to provide them somehow.
Or if rather you have mismatches of versions then you need to review what are the duplicated entries in the class path.

No idea what Resin is, but yes you can create containers like Docker; that is probably what most of us do.

And for that you have a couple of options:

  • Using sbt-assembly + a custom Dockerfile
    And call docker build after a sbt assembly
  • Using sbt-native-packager + its Docker plugin.

You may also explore using Graal to create native images. This is great for small scripts / programs and CLI tools.

2 Likes