---
title: Create a Databricks compatible JAR
description: "Create a Databricks-compatible JAR for Lakeflow Jobs: match JDK, Scala, and Spark versions, use the Databricks Spark session, and allowlist the JAR."
canonical_url: https://docs.databricks.com/aws/en/jobs/tasks/jar-create
---

# Create a Databricks compatible JAR

A Java archive (JAR) packages Java or Scala code for deployment in Lakeflow Jobs. Meet the JAR compatibility requirements and configure your project for the target compute type.

> [!TIP]
>
> For automated deployment and continuous integration workflows, use Declarative Automation Bundles to create a project from a template with pre-configured build and deployment settings. See [Build a Scala JAR using Declarative Automation Bundles](https://docs.databricks.com/aws/en/dev-tools/bundles/scala-jar.md) and [Bundle that uploads a JAR file to Unity Catalog](https://docs.databricks.com/aws/en/dev-tools/bundles/examples.md#jar-upload). This page describes the manual approach for understanding JAR requirements and custom configurations.

At a high level, your JAR must meet the following requirements for compatibility:

* **Match versions**: Use the same Java Development Kit (JDK), Scala, and Spark versions as your compute
* **Provide dependencies**: Include required libraries in your JAR or install them on your compute
* **Use the Databricks Spark session**: Call `SparkSession.builder().getOrCreate()` to access the session
* **Use supported thread pools** (dedicated compute with Unity Catalog only): Use a supported `ThreadUtils` thread pool, such as `ThreadUtils.newDaemonFixedThreadPool`. Other thread pools are not supported.
* **Allowlist your JAR** (standard compute only): Add your JAR to the [allowlist](https://docs.databricks.com/aws/en/data-governance/unity-catalog/manage-privileges/allowlist.md)

## <a id="compute-architecture" />Compute architecture

**Serverless and standard compute** use [Spark Connect architecture](https://docs.databricks.com/aws/en/compute/lakeguard.md#lakeguard-architecture) to isolate user code and enforce Unity Catalog governance. Databricks Connect includes the Spark Connect APIs. Serverless and standard compute don't support Spark Context or Spark RDD APIs directly. See [serverless limitations](https://docs.databricks.com/aws/en/compute/serverless/limitations.md) and [standard access mode limitations](https://docs.databricks.com/aws/en/compute/standard-limitations.md).

**Dedicated compute** uses the [classic Spark architecture](https://docs.databricks.com/aws/en/compute/lakeguard.md#classic-spark-architecture) and includes all Spark APIs.

## <a id="find-versions" />Find your JDK, Scala, and Spark versions

Match JDK, Scala, and Spark versions running on your compute

When you build a JAR, your JDK, Scala, and Spark versions must match the versions running on your compute. These three versions are interconnected - the Spark version determines the compatible Scala version, and both depend on a specific JDK version.

Follow these steps to find the correct versions for your compute type:

### Serverless

1. Use [serverless environment version](https://docs.databricks.com/aws/en/release-notes/serverless/environment-version/index.md) 4 or higher

2. Find the Databricks Connect version, JDK, and Scala versions for your environment in the [serverless environment versions](https://docs.databricks.com/aws/en/release-notes/serverless/environment-version/index.md) table.

### Standard

1. Click ![Cloud icon.](https://docs.databricks.com/aws/en/images/product-icons/CloudIcon.svg) **Compute** in the sidebar and select your compute to view the Databricks Runtime version
2. Use a Databricks Connect version that matches the Databricks Runtime major and minor version (for example, Databricks Runtime 17.3 → databricks-connect 17.x). Find the corresponding JDK and Scala versions in the [version support matrix](https://docs.databricks.com/aws/en/dev-tools/databricks-connect/requirements.md#versions).

### Dedicated

1. Click ![Cloud icon.](https://docs.databricks.com/aws/en/images/product-icons/CloudIcon.svg) **Compute** in the sidebar and select your compute to view the Databricks Runtime version
2. Find the JDK, Scala, and Spark versions in the **System environment** section of the [release notes](https://docs.databricks.com/aws/en/release-notes/runtime/index.md) for your Databricks Runtime version (for example, [Databricks Runtime 17.3 LTS](https://docs.databricks.com/aws/en/release-notes/runtime/17.3lts.md#system-environment))

> [!NOTE]
>
> Using mismatched JDK, Scala, or Spark versions can cause unexpected behavior or prevent your code from running.

## <a id="project-setup" />Project setup

After you know your version requirements, configure your build files and package your JAR.

### <a id="set-versions" />Set JDK and Scala versions

Configure your build file to use the correct JDK and Scala versions. The following examples show the versions for Databricks Runtime 17.3 LTS and [serverless environment version 4](https://docs.databricks.com/aws/en/release-notes/serverless/environment-version/four.md).

#### Sbt

In `build.sbt`:

```scala
scalaVersion := "2.13.16"

javacOptions ++= Seq("-source", "17", "-target", "17")
```

#### Maven

In `pom.xml`:

```xml
<properties>
  <scala.version>2.13.16</scala.version>
  <scala.binary.version>2.13</scala.binary.version>
  <maven.compiler.source>17</maven.compiler.source>
  <maven.compiler.target>17</maven.compiler.target>
</properties>
```

### <a id="dependencies" /><a id="spark" />Spark dependencies

Add a Spark dependency to access Spark APIs without packaging Spark in your JAR.

#### Serverless

**Use Databricks Connect**

Add a dependency on Databricks Connect (recommended). The Databricks Connect version must match the Databricks Connect version in your [serverless environment](https://docs.databricks.com/aws/en/release-notes/serverless/environment-version/index.md). Mark it as `provided` because it's included in the runtime. Don't include Apache Spark dependencies like `spark-core` or other `org.apache.spark` artifacts in your build file. Databricks Connect has all necessary Spark APIs.

Maven `pom.xml`:

```xml
<dependency>
  <groupId>com.databricks</groupId>
  <artifactId>databricks-connect_2.13</artifactId>
  <version>17.3.2</version>
  <scope>provided</scope>
</dependency>
```

sbt `build.sbt`:

```scala
libraryDependencies += "com.databricks" %% "databricks-connect" % "17.3.+" % "provided"
```

**Alternative: spark-sql-api**

You can compile against `spark-sql-api` instead of Databricks Connect, but Databricks recommends using Databricks Connect because the Spark APIs running on serverless compute can differ slightly from open-source Spark. These libraries are included in the runtime, so mark them as `provided`.

Maven `pom.xml`:

```xml
<dependency>
  <groupId>org.apache.spark</groupId>
  <artifactId>spark-sql-api</artifactId>
  <version>4.0.1</version>
  <scope>provided</scope>
</dependency>
```

sbt `build.sbt`:

```scala
libraryDependencies += "org.apache.spark" %% "spark-sql-api" % "4.0.1" % "provided"
```

#### Standard access mode

**Use Databricks Connect**

Add a dependency on Databricks Connect (recommended). The Databricks Connect version must match the major and minor Databricks Runtime version of your cluster (for example, Databricks Runtime 17.3 → databricks-connect 17.x). Mark it as `provided` because it's included in the runtime. Don't include Apache Spark dependencies like `spark-core` or other `org.apache.spark` artifacts in your build file. Databricks Connect has all necessary Spark APIs.

Maven `pom.xml`:

```xml
<dependency>
  <groupId>com.databricks</groupId>
  <artifactId>databricks-connect_2.13</artifactId>
  <version>17.3.2</version>
  <scope>provided</scope>
</dependency>
```

sbt `build.sbt`:

```scala
libraryDependencies += "com.databricks" %% "databricks-connect" % "17.3.+" % "provided"
```

**Alternative: spark-sql-api**

You can compile against `spark-sql-api` instead of Databricks Connect, but Databricks recommends using Databricks Connect because the Spark APIs running on serverless compute might differ slightly from open-source Spark. These libraries are included in the runtime, so mark them as `provided`.

Maven `pom.xml`:

```xml
<dependency>
  <groupId>org.apache.spark</groupId>
  <artifactId>spark-sql-api</artifactId>
  <version>4.0.1</version>
  <scope>provided</scope>
</dependency>
```

sbt `build.sbt`:

```scala
libraryDependencies += "org.apache.spark" %% "spark-sql-api" % "4.0.1" % "provided"
```

#### Dedicated access mode

**Use Databricks Connect or Spark APIs**

Add a dependency on Databricks Connect (recommended) or compile against Spark libraries with scope `provided`.

Option 1: **databricks-connect (recommended)**

Mark it as `provided` because it's included in the runtime.

Maven `pom.xml`:

```xml
<dependency>
  <groupId>com.databricks</groupId>
  <artifactId>databricks-connect_2.13</artifactId>
  <version>17.3.2</version>
  <scope>provided</scope>
</dependency>
```

sbt `build.sbt`:

```scala
libraryDependencies += "com.databricks" %% "databricks-connect" % "17.3.+" % "provided"
```

Option 2: **spark-sql-api**

You can compile against `spark-sql-api`, but this isn't recommended because the version on Databricks might differ slightly. These libraries are included in the runtime, so mark them as `provided`.

Maven `pom.xml`:

```xml
<dependency>
  <groupId>org.apache.spark</groupId>
  <artifactId>spark-sql-api</artifactId>
  <version>4.0.1</version>
  <scope>provided</scope>
</dependency>
```

sbt `build.sbt`:

```scala
libraryDependencies += "org.apache.spark" %% "spark-sql-api" % "4.0.1" % "provided"
```

Option 3: **Other Spark libraries**

You can use any Apache Spark library (for example, `spark-core` or `spark-sql`) with scope `provided`, as long as the version matches the Spark version running on your cluster. Find your cluster's Spark version in the **System environment** section of the [Databricks Runtime release notes](https://docs.databricks.com/aws/en/release-notes/runtime/index.md).

### <a id="application-dependencies" />Application dependencies

Add your application's required libraries to your build file. How you manage these depends on your compute type:

#### Serverless

Serverless compute provides Databricks Connect and a limited set of dependencies ([see release notes](https://docs.databricks.com/aws/en/release-notes/serverless/environment-version/index.md)). Package all other libraries in your JAR using [sbt-assembly](https://github.com/sbt/sbt-assembly) or [Maven Shade Plugin](https://maven.apache.org/plugins/maven-shade-plugin/), or add them to your [serverless environment](https://docs.databricks.com/aws/en/compute/serverless/dependencies.md#libraries).

For example, to package a library in your JAR:

Maven `pom.xml`:

```xml
<dependency>
  <groupId>io.circe</groupId>
  <artifactId>circe-core_2.13</artifactId>
  <version>0.14.10</version>
</dependency>
```

sbt `build.sbt`:

```scala
libraryDependencies += "io.circe" %% "circe-core" % "0.14.10"
```

#### Standard access mode

The Databricks Runtime includes many common libraries beyond Spark. Find the complete list of provided libraries and versions in the **System Environment** section of the [Databricks Runtime release notes](https://docs.databricks.com/aws/en/release-notes/runtime/index.md) for your Databricks Runtime version (for example, [Databricks Runtime 17.3 LTS](https://docs.databricks.com/aws/en/release-notes/runtime/17.3lts.md#system-environment)).

For libraries provided by Databricks Runtime, add them as dependencies with scope `provided`. For example, in Databricks Runtime 17.3 LTS, `protobuf-java` is provided:

Maven `pom.xml`:

```xml
<dependency>
  <groupId>com.google.protobuf</groupId>
  <artifactId>protobuf-java</artifactId>
  <version>3.25.5</version>
  <scope>provided</scope>
</dependency>
```

sbt `build.sbt`:

```scala
libraryDependencies += "com.google.protobuf" % "protobuf-java" % "3.25.5" % "provided"
```

For libraries not provided by Databricks Runtime, either package them in your JAR using [sbt-assembly](https://github.com/sbt/sbt-assembly) or [Maven Shade Plugin](https://maven.apache.org/plugins/maven-shade-plugin/), or install them as [compute-scoped libraries](https://docs.databricks.com/aws/en/libraries/index.md#compatibility).

#### Dedicated access mode

The Databricks Runtime includes many common libraries beyond Spark. Find the complete list of provided libraries and versions in the **System Environment** section of the [Databricks Runtime release notes](https://docs.databricks.com/aws/en/release-notes/runtime/index.md) for your Databricks Runtime version (for example, [Databricks Runtime 17.3 LTS](https://docs.databricks.com/aws/en/release-notes/runtime/17.3lts.md#system-environment)).

For libraries provided by Databricks Runtime, add them as dependencies with scope `provided`. For example, in Databricks Runtime 17.3 LTS, `protobuf-java` is provided:

Maven `pom.xml`:

```xml
<dependency>
  <groupId>com.google.protobuf</groupId>
  <artifactId>protobuf-java</artifactId>
  <version>3.25.5</version>
  <scope>provided</scope>
</dependency>
```

sbt `build.sbt`:

```scala
libraryDependencies += "com.google.protobuf" % "protobuf-java" % "3.25.5" % "provided"
```

For libraries not provided by Databricks Runtime, either package them in your JAR using [sbt-assembly](https://github.com/sbt/sbt-assembly) or [Maven Shade Plugin](https://maven.apache.org/plugins/maven-shade-plugin/), or install them as [compute-scoped libraries](https://docs.databricks.com/aws/en/libraries/index.md#compatibility).

## <a id="code-requirements" />Code requirements

When writing your JAR code, follow these patterns to ensure compatibility with Databricks jobs.

### <a id="spark-session" />Use the Databricks Spark session

When running a JAR in a job, you must use the Spark session provided by Databricks. The following code shows how to access the session from your code:

#### Java

```java
SparkSession spark = SparkSession.builder().getOrCreate();
```

#### Scala

```scala
val spark = SparkSession.builder().getOrCreate()
```

### <a id="thread-pools" />Use supported thread pools with Unity Catalog

For JAR tasks on dedicated compute that access Unity Catalog data, use a supported thread pool in `org.apache.spark.util.ThreadUtils`, such as `ThreadUtils.newDaemonFixedThreadPool` or `ThreadUtils.newDaemonCachedThreadPool`.

Other thread pools are not supported, including `ForkJoinPool`, Scala parallel collections such as `.par`, `ThreadUtils.newForkJoinPool`, and any `ScheduledExecutorService` thread pool. See [ Limitations](https://docs.databricks.com/aws/en/data-governance/unity-catalog/requirements.md#limitations).

### <a id="cleanup" />Use `try-finally` blocks for job cleanup

If you want code that reliably runs at the end of your job, for example, to clean up temporary files, use a `try-finally` block. Don't use a shutdown hook, because these don't run reliably in jobs.

Consider a JAR that consists of two parts:

* `jobBody()` which contains the main part of the job.
* `jobCleanup()` which must run after `jobBody()`, whether that function succeeds or returns an exception.

For example, `jobBody()` creates tables and `jobCleanup()` drops those tables.

The safe way to ensure that the clean-up method is called is to put a `try-finally` block in the code:

```scala
try {
  jobBody()
} finally {
  jobCleanup()
}
```

Don't try to clean up using `sys.addShutdownHook(jobCleanup)` or the following code:

```scala
// Do NOT clean up with a shutdown hook like this. This will fail.
val cleanupThread = new Thread { override def run = jobCleanup() }
Runtime.getRuntime.addShutdownHook(cleanupThread)
```

Databricks manages Spark container lifetimes in a way that prevents shutdown hooks from running reliably.

### <a id="jar-job-parameters" />Read job parameters

Databricks passes parameters to your JAR job as a JSON string array. To use these parameters, inspect the `String` array passed into your `main` function.

For more details on parameters, see [Parameterize jobs](https://docs.databricks.com/aws/en/jobs/parameters.md).

## <a id="additional-configuration" />Additional configuration

Depending on your compute type, you might need additional configuration:

* **Standard access mode**: For security reasons, an administrator must add Maven coordinates and paths for JAR libraries to an [allowlist](https://docs.databricks.com/aws/en/data-governance/unity-catalog/manage-privileges/allowlist.md).
* **Serverless compute**: If your job accesses private resources (databases, APIs, storage), configure networking with a Network Connectivity Configuration (NCC). See [Serverless network security](https://docs.databricks.com/aws/en/security/network/serverless-network-security/index.md).

## <a id="logging" />Configure logging for serverless compute

On serverless compute, the SLF4J logging API uses a no-operation (NOP) backend by default. This means log messages from libraries and application code that use SLF4J are silently discarded.

To route SLF4J log output to the Log4j 2 backend, you must add the `log4j-slf4j2-impl` bridge to your fat JAR or as a separate JAR dependency on your job. The serverless environment includes the Log4j 2 backend (`log4j-api` and `log4j-core`), but the bridge that connects SLF4J to Log4j is not on the user classpath by default.

The bridge version must match the `log4j-api` version shipped in your [serverless environment version](https://docs.databricks.com/aws/en/release-notes/serverless/environment-version/index.md). For example, [environment version 5](https://docs.databricks.com/aws/en/release-notes/serverless/environment-version/five.md) uses `log4j-api` version `2.20.0`, so you must add `log4j-slf4j2-impl` version `2.20.0`.

### Option 1: Include the bridge in your fat JAR

Add `log4j-slf4j2-impl` as a compile dependency so it is packaged into your fat JAR:

#### Sbt

In `build.sbt`:

```scala
libraryDependencies += "org.apache.logging.log4j" % "log4j-slf4j2-impl" % "2.20.0"
```

#### Maven

In `pom.xml`:

```xml
<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-slf4j2-impl</artifactId>
  <version>2.20.0</version>
</dependency>
```

### Option 2: Add the bridge as a separate JAR dependency on the job

Instead of packaging the bridge in your fat JAR, you can add it as a separate library dependency when you configure your JAR task. In the task configuration under **Environment and Libraries**, add a JAR dependency using the Maven coordinate:

```
org.apache.logging.log4j:log4j-slf4j2-impl:2.20.0
```

For details on adding JAR dependencies to serverless jobs, see [Configure the serverless environment](https://docs.databricks.com/aws/en/compute/serverless/dependencies.md).

> [!NOTE]
>
> Do not include `log4j-api` or `log4j-core` in your fat JAR. These libraries are already provided by the serverless runtime, and bundling them can cause version conflicts.

## <a id="next-steps" />Additional resources

* Learn how to [use a jar in a job](https://docs.databricks.com/aws/en/jobs/how-to/use-jars-in-workflows.md).
* Learn about [Databricks Connect](https://docs.databricks.com/aws/en/dev-tools/databricks-connect/index.md).
* Learn about [Scala in Databricks](https://docs.databricks.com/aws/en/languages/scala.md).
