0

I have encountered with this error while building Scala JAR file, please help me.

build.sbt file:

cat > databricks_jar_test/build.sbt << EOF
ThisBuild / scalaVersion := "2.12.14"
ThisBuild / organization := "com.nm.udf"

lazy val udfDecrypt = (project in file("."))
  .settings(
    name := "udfDecrypt"
  )
// https://mvnrepository.com/artifact/org.scala-lang/scala-library
libraryDependencies += "com.macasaet.fernet" % "fernet-java8" % "1.5.0"
// https://mvnrepository.com/artifact/org.apache.hive/hive-exec
libraryDependencies += "org.apache.hive" % "hive-exec" % "3.1.2"

Scala file

cat > databricks_jar_test/src/main/scala/udfDecrypt.scala << EOF
package com.nm.udf;
import java.time.Duration;
import java.time.Instant;
import com.macasaet.fernet.{Key, StringValidator, Token};
import org.apache.hadoop.hive.ql.exec.UDF;
import java.time.{Duration, Instant};

class Validator extends StringValidator {
    override def getTimeToLive() : java.time.temporal.TemporalAmount = {
      Duration.ofSeconds(Instant.MAX.getEpochSecond());
    }
  }

class udfDecrypt extends UDF {
    def evaluate(inputVal: String, sparkKey : String): String = {
      if( inputVal != null && inputVal!="" ) {
        val keys: Key = new Key(sparkKey)
        val token = Token.fromString(inputVal)
        val validator = new Validator() {}
        val payload = token.validateAndDecrypt(keys, validator)
        payload
      } else return inputVal
    }
  }
cd databricks_jar_test/
sbt compile
cd databricks_jar_test/
sbt "run Hello World\!"
# Hello, World! 

[error] java.lang.RuntimeException: No main class detected. [error] at scala.sys.package$.error(package.scala:30) [error] stack trace is suppressed; run 'last Compile / bgRun' for the full output [error] (Compile / bgRun) No main class detected. [error] Total time: 3 s, completed Jun 1, 2023, 3:06:00 PM

CalledProcessError Traceback (most recent call last) <ipython-input-11-2a66b8e4a500> in <cell line: 1>() 1 get_ipython().run_cell_magic('bash', '', 'cd databricks_jar_test/\nsbt "run Hello World\!"\n# Hello, World! \n')

4 frames /usr/local/lib/python3.10/dist-packages/google/colab/_shell.py in run_cell_magic(self, magic_name, line, cell) 332 if line and not cell: 333 cell = ' ' 334 return super().run_cell_magic(magic_name, line, cell) 335 336

/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py in run_cell_magic(self, magic_name, line, cell) 2471 with self.builtin_trap: 2472 args = (magic_arg_s, cell) 2473 result = fn(*args, **kwargs) 2474 return result 2475

/usr/local/lib/python3.10/dist-packages/IPython/core/magics/script.py in named_script_magic(line, cell) 140 else: 141 line = script 142 return self.shebang(line, cell) 143 144 # write a basic docstring:

<decorator-gen-103> in shebang(self, line, cell)

/usr/local/lib/python3.10/dist-packages/IPython/core/magic.py in <lambda>(f, *a, **k) 185 # but it's overkill for just that one bit of state. 186 def magic_deco(arg): 187 call = lambda f, *a, **k: f(*a, **k) 188 189 if callable(arg):

/usr/local/lib/python3.10/dist-packages/IPython/core/magics/script.py in shebang(self, line, cell) 243 sys.stderr.flush() 244 if args.raise_error and p.returncode!=0: 245 raise CalledProcessError(p.returncode, cell, output=out, stderr=err) 246 247 def _run_script(self, p, cell, to_close):

CalledProcessError: Command 'b'cd databricks_jar_test/\nsbt "run Hello World\!"\n# Hello, World! \n'' returned non-zero exit status 1.

This is for the encryption. I have tried in Databricks and Colab, same problem.

Gaël J
  • 11,274
  • 4
  • 17
  • 32
Uyên Lê
  • 1
  • 1
  • What are you trying to achieve? It is true that there's no main class and thus you cannot run this code like this. This code looks like a library that would be used by some other project. – Gaël J Jun 02 '23 at 05:59
  • I want to use as in this tutorial: https://www.databricks.com/notebooks/enforcing-column-level-encryption.html – Uyên Lê Jun 02 '23 at 06:27
  • Okay, so you don't need to run the JAR yourself. You need to package this Scala code to a JAR. Search for "SBT package jar" on your search engine, it should give you some directions and solutions. – Gaël J Jun 02 '23 at 09:11
  • Basically `sbt package` should be enough in your case. Look at /questions/20938177/create-standalone-jar-using-sbt for instance. – Gaël J Jun 02 '23 at 09:13
  • You need to specify the main class in sbt https://stackoverflow.com/a/18198743/5122436 to run it via java after build. But commonly `sbt run`can find main classes automatically. – Mikhail Ionkin Jun 02 '23 at 20:51

0 Answers0