---
title: Create and run JARs on serverless compute
description: Package Java or Scala code in a JAR and run it as a JAR task on serverless compute in a Lakeflow Job.
canonical_url: https://docs.databricks.com/aws/en/jobs/how-to/use-jars-in-workflows
---

# Create and run JARs on serverless compute

> [!IMPORTANT]
>
> Databricks strongly recommends Declarative Automation Bundles instead of building and deploying JARs manually as described on this page. Declarative Automation Bundles makes it easy to create a project from a template that has the correct Scala, JDK, and Databricks Connect versions already configured for serverless, and also enables simple deployment of the JAR to the Databricks workspace. See [Build a Scala JAR with Declarative Automation Bundles](https://docs.databricks.com/aws/en/dev-tools/bundles/scala-jar.md).

A [Java archive](https://en.wikipedia.org/wiki/JAR_\(file_format\)) (JAR) packages Java or Scala code into a single file. Create a JAR with Spark code and deploy it as a JAR task on [serverless compute](https://docs.databricks.com/aws/en/jobs/run-serverless-jobs.md) in a Lakeflow Job.

## <a id="requirements" />Requirements

To build a JAR, your local development environment must have the following installed:

* [sbt](https://www.scala-sbt.org/download/) 1.11.7 or higher for Scala JARs
* [Maven](https://maven.apache.org/install.html) 3.9.0 or higher for Java JARs
* JDK, Scala, and Databricks Connect versions that match your [serverless environment](https://docs.databricks.com/aws/en/release-notes/serverless/environment-version/index.md). See [Dependency versions](https://docs.databricks.com/aws/en/jobs/how-to/use-jars-in-workflows.md#dependency-versions).

### <a id="dependency-versions" />Dependency versions

> [!IMPORTANT]
>
> To run on serverless compute without failures, your JAR Scala and JDK versions must exactly match the runtime Scala and JDK versions. See [Databricks Connect versions](https://docs.databricks.com/aws/en/dev-tools/databricks-connect/requirements.md#versions).

The example on this page uses [serverless environment version 4](https://docs.databricks.com/aws/en/release-notes/serverless/environment-version/index.md), so this page creates a JAR that:

* Is compiled against Scala 2.13; every dependency uses the `_2.13` suffix.
* Is compiled against JDK 17, class file version 61.
* Is compiled against Databricks Connect 17.3, the Spark API surface for serverless compute.
* Uses only public Spark APIs. It uses no RDDs and no Spark internals. See [Limitations](https://docs.databricks.com/aws/en/jobs/how-to/use-jars-in-workflows.md#limitations).
* Includes every dependency in the JAR or attached as a serverless environment library. See [Managing dependencies](https://docs.databricks.com/aws/en/jobs/how-to/use-jars-in-workflows.md#dependencies).

#### <a id="limitations" />Limitations

Serverless compute uses [Spark Connect](https://docs.databricks.com/aws/en/compute/lakeguard.md#lakeguard-architecture). Your JAR runs against a thin client library that exposes the public Spark APIs, while the Spark engine itself runs server-side. Code that bypasses the public API can't benefit from Catalyst optimization or [Photon](https://docs.databricks.com/aws/en/compute/photon.md) acceleration, even on classic compute. RDD-based and internals-dependent code is generally slower than the equivalent DataFrame or SQL code.

The following aren't available:

* RDD API (`org.apache.spark.rdd.*`) and `SparkContext` / `JavaSparkContext`. Use `SparkSession.builder().getOrCreate()` and DataFrame/Dataset operations instead.
* Spark internal APIs (`org.apache.spark.catalyst.*`, `org.apache.spark.util.*`, `org.apache.spark.sql.util.*`, `org.apache.spark.sql.internal.*`). Code that imports these APIs fail with `NoClassDefFoundError`. Refactor to the public Spark API. If a third-party library uses internals, check whether it publishes a Spark Connect-compatible release.
* Native libraries (`.so`, `.dll`, JNI). Serverless compute does not permit writing native libraries to the file system. Libraries that unpack native binaries at startup fail with `UnsatisfiedLinkError`. Init scripts are not a workaround. Use a Java equivalent if one is available.

If your workload requires any of the above, run it on [standard or dedicated compute](https://docs.databricks.com/aws/en/compute/index.md) instead.

## <a id="example" />Step 1: Build a JAR

### Scala

1. Run the following command to create a Scala project:

   ```bash
   sbt new scala/scala-seed.g8
   ```

   When prompted, enter a project name, for example, `my-spark-app`.

2. Next, delete the seed's stub files and create the directory for your source:

   ```bash
   cd my-spark-app
   rm src/main/scala/example/Hello.scala
   rm src/test/scala/example/HelloSpec.scala
   rm project/Dependencies.scala
   mkdir -p src/main/scala/com/examples
   ```

3. Replace the contents of your `build.sbt` file with the following:

   ```scala
   name := "my-spark-app"

   // Set the dependency versions
   scalaVersion := "2.13.16"
   javacOptions ++= Seq("--release", "17")
   scalacOptions ++= Seq("-release", "17")

   libraryDependencies += "com.databricks" %% "databricks-connect" % "17.3.2" % "provided"
   // Your other dependencies go here. Use %% for Scala libraries so sbt picks the _2.13 artifact.

   // Fork a new JVM on run so our javaOptions are applied.
   fork := true
   javaOptions += "--add-opens=java.base/java.nio=ALL-UNNAMED"
   ```

4. Edit or create a `project/plugins.sbt` file, and add this line:

   ```scala
   addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.3.1")
   ```

5. Create your main class in `src/main/scala/com/examples/SparkJar.scala`:

   ```scala
   package com.examples

   import org.apache.spark.sql.SparkSession

   object SparkJar {
     def main(args: Array[String]): Unit = {
       val spark = SparkSession.builder().getOrCreate()

       // Prints the arguments to the class, which
       // are job parameters when run as a job:
       println(args.mkString(", "))

       // Shows using spark:
       println(spark.version)
       println(spark.range(10).limit(3).collect().mkString(" "))
     }
   }
   ```

6. To build your JAR file, run the following command:

   ```bash
   sbt assembly
   ```

   The compiled JAR is created in the `target/` folder as `my-spark-app-assembly-0.1.0-SNAPSHOT.jar`.

### Java

1. Run the following commands to create a Maven project structure:

   ```bash
   mkdir -p my-spark-app/src/main/java/com/examples
   cd my-spark-app
   ```

2. Create a `pom.xml` file in the project root with the following contents:

   ```xml
   <?xml version="1.0" encoding="UTF-8"?>
   <project xmlns="http://maven.apache.org/POM/4.0.0"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
            http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>

     <groupId>com.examples</groupId>
     <artifactId>my-spark-app</artifactId>
     <version>1.0-SNAPSHOT</version>

     <properties>
       <maven.compiler.release>17</maven.compiler.release>
       <scala.binary.version>2.13</scala.binary.version>
       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     </properties>

     <dependencies>
       <!-- Included on serverless compute. -->
       <dependency>
         <groupId>com.databricks</groupId>
         <artifactId>databricks-connect_${scala.binary.version}</artifactId>
         <version>17.3.2</version>
         <scope>provided</scope>
       </dependency>
     </dependencies>

     <build>
       <plugins>
         <!-- Maven Shade Plugin - Creates a fat JAR with all non-provided dependencies. -->
         <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-shade-plugin</artifactId>
           <version>3.6.1</version>
           <executions>
             <execution>
               <phase>package</phase>
               <goals>
                 <goal>shade</goal>
               </goals>
               <configuration>
                 <transformers>
                   <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                     <mainClass>com.examples.SparkJar</mainClass>
                   </transformer>
                 </transformers>
               </configuration>
             </execution>
           </executions>
         </plugin>
       </plugins>
     </build>
   </project>
   ```

3. Create your main class in `src/main/java/com/examples/SparkJar.java`:

   ```java
   package com.examples;

   import org.apache.spark.sql.SparkSession;
   import java.util.stream.Collectors;

   public class SparkJar {
     public static void main(String[] args) {
       SparkSession spark = SparkSession.builder().getOrCreate();

       // Prints the arguments to the class, which
       // are job parameters when run as a job:
       System.out.println(String.join(", ", args));

       // Shows using spark:
       System.out.println(spark.version());
       System.out.println(
         spark.range(10).limit(3).collectAsList().stream()
           .map(Object::toString)
           .collect(Collectors.joining(" "))
       );
     }
   }
   ```

4. To build your JAR file, run the following command:

   ```bash
   mvn clean package
   ```

   The compiled JAR is created in the `target/` folder as `my-spark-app-1.0-SNAPSHOT.jar`.

### <a id="dependencies" />Manage dependencies

To make a library available to your JAR on serverless compute:

* **Use a [provided library](https://docs.databricks.com/aws/en/jobs/how-to/use-jars-in-workflows.md#provided-libraries)**: Serverless compute includes Databricks Connect and a curated set of common libraries. If your version is compatible, declare it `provided` in your build and don't include it in your JAR.
* **Attach as an environment library**: Add a library to your [serverless environment](https://docs.databricks.com/aws/en/compute/serverless/dependencies.md#libraries) if it isn't already provided. Use this for runtime-only libraries you don't want to include.
* **Connect to an external database**: For JDBC sources, use a [JDBC connection](https://docs.databricks.com/aws/en/connect/jdbc-connection.md) instead of including a driver. JDBC connections are Unity Catalog-managed. Credentials, lineage, and governance are handled for you.

#### <a id="provided-libraries" />Provided libraries

The following libraries are required dependencies and are available by default on serverless compute. Declare them `provided` in your build. Bundling your own versions of these libraries triggers a `NoSuchMethodError` at runtime.

> [!NOTE]
>
> The library versions listed below are for **serverless environment version 4**. For installed libraries for other environment versions, see the [serverless environment version notes](https://docs.databricks.com/aws/en/release-notes/serverless/environment-version/index.md) reference.

* `com.databricks:databricks-connect_2.13`, version 17.3.2
* `org.scala-lang:scala-library_2.13`, version 2.13.16
* `org.scala-lang:scala-reflect_2.13`, version 2.13.16
* `org.slf4j:slf4j-api`, version 2.0.10
* `org.apache.logging.log4j:log4j-api`, version 2.20.0
* `org.apache.logging.log4j:log4j-core`, version 2.20.0
* `org.apache.httpcomponents:httpclient`, version 4.5.14
* `org.apache.httpcomponents:httpcore`, version 4.4.16
* `com.fasterxml.jackson.core:jackson-databind`, version 2.15.2
* `com.fasterxml.jackson.core:jackson-core`, version 2.15.2
* `com.fasterxml.jackson.core:jackson-annotations`, version 2.15.2
* `com.fasterxml.jackson.datatype:jackson-datatype-jsr310`, version 2.15.2
* `com.google.guava:guava`, version 32.0.1-jre
* `commons-io:commons-io`, version 2.14.0
* `org.json4s:json4s-jackson_2.13`, version 4.0.7
* `org.apache.commons:commons-lang3`, version 3.14.0
* `org.apache.commons:commons-configuration2`, version 2.11.0
* `org.apache.commons:commons-text`, version 1.12.0
* `com.databricks:databricks-sdk-java`, version 0.52.0
* `com.databricks:databricks-dbutils-scala_2.13`, version 0.1.4

## <a id="create-job" />Step 2: Create a job to run the JAR

1. In your workspace, click ![Workflows icon.](https://docs.databricks.com/aws/en/images/product-icons/WorkflowsIcon.svg) **Jobs & Pipelines** in the sidebar.

2. Click **Create**, then **Job**.

3. Click the **JAR** tile to configure the first task. If the **JAR** tile is not available, click **Add another task type** and search for **JAR**.

4. Optionally, replace the name of the job, which defaults to **`New Job <date-time>`**, with your job name.

5. In **Task name**, enter a name for the task, for example `JAR_example`.

6. If necessary, select **JAR** from the **Type** drop-down menu.

7. For **Main class**, enter the package and class of your JAR. If you followed the example earlier, enter `com.examples.SparkJar`.

8. For **Compute**, select **Serverless**.

9. Configure the serverless environment:
   1. Select an environment, then click ![Pencil icon.](https://docs.databricks.com/aws/en/images/product-icons/PencilIcon.svg) **Edit** to configure it.
   2. Select **4** or higher for the **Environment version**.
   3. Add your JAR file by dragging and dropping it into the file selector, or browse to select it from a Unity Catalog volume or workspace location.

10. For **Parameters**, for this example, enter `["Hello", "World!"]`.

11. Click **Save task**.

## <a id="run-job" />Step 3: Run the job and view the job run details

Click ![Run Now Button](https://docs.databricks.com/aws/en/images/jobs/run-now-button.png) to run the workflow. To view [details for the run](https://docs.databricks.com/aws/en/jobs/monitor.md#job-run-details), click **View run** in the **Triggered run** pop-up or click the link in the **Start time** column for the run in the [job runs](https://docs.databricks.com/aws/en/jobs/monitor.md#view-job-run-list) view.

When the run completes, the output appears in the **Output** pane, including the arguments you passed to the task.

## <a id="troubleshooting" />Troubleshooting

The following table provides troubleshooting information for common exceptions.

| Exception                                                                                                                                   | Cause                                                                                                                                                                  | Fix                                                                                                                                                                         |
| ------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `NoSuchMethodError` referencing a `scala.*` class                                                                                           | JAR compiled against Scala 2.12; serverless runs Scala 2.13                                                                                                            | Recompile with `scalaVersion := "2.13.16"`. Ensure every Scala dependency uses the `_2.13` cross-version suffix.                                                            |
| `NoClassDefFoundError: scala/...`                                                                                                           | Scala 2.12 vs 2.13 mismatch                                                                                                                                            | Recompile with `scalaVersion := "2.13.16"`. Ensure every Scala dependency uses the `_2.13` cross-version suffix.                                                            |
| `UnsupportedClassVersionError` (a class file version higher than 61)                                                                        | Compiled with JDK 18 or higher; serverless runs JDK 17                                                                                                                 | Use `<maven.compiler.release>17</maven.compiler.release>` (Maven) or `--release 17` (sbt / javac)                                                                           |
| `NoClassDefFoundError: org/apache/spark/...` for an internal package (`catalyst`, `util`, `sql/util`, `sql/internal`, `api/java`, or `rdd`) | Spark internals or RDD API were used. These are not available on serverless.                                                                                           | Use the public Spark API (DataFrame/Dataset/SQL). See [limitations on serverless](https://docs.databricks.com/aws/en/jobs/how-to/use-jars-in-workflows.md#limitations).     |
| `ClassNotFoundException` for a JDBC driver class (for example, `oracle.jdbc.OracleDriver`)                                                  | JDBC driver not on classpath                                                                                                                                           | Use a [JDBC connection](https://docs.databricks.com/aws/en/connect/jdbc-connection.md) for the external database.                                                           |
| `ClassNotFoundException` for a third-party class (for example, `kotlin.jvm.internal.*`)                                                     | The library is not on the serverless classpath.                                                                                                                        | Add it to your JAR, or provide it as an additional JAR using the [serverless environment](https://docs.databricks.com/aws/en/compute/serverless/dependencies.md#libraries). |
| `UnsatisfiedLinkError` referencing a file under `/tmp/`                                                                                     | Native library included in JAR                                                                                                                                         | Native libraries are not supported on serverless. Use a pure-Java equivalent, or run on classic compute.                                                                    |
| `NoSuchMethodError` from a third-party library (Apache Commons, Guava, Jackson, etc.)                                                       | Your included version conflicts with the [version provided by serverless](https://docs.databricks.com/aws/en/jobs/how-to/use-jars-in-workflows.md#provided-libraries). | Use the provided version. Mark it `provided` in your build and don't include it in your JAR.                                                                                |

## <a id="learn-more" />Next steps

* To learn more about JAR tasks, see [JAR task for jobs](https://docs.databricks.com/aws/en/jobs/tasks/jar.md).
* To learn more about creating a compatible JAR, see [Create a Databricks compatible JAR](https://docs.databricks.com/aws/en/jobs/tasks/jar-create.md).
* To learn more about creating and running jobs, see [Lakeflow Jobs](https://docs.databricks.com/aws/en/jobs/index.md).
