Skip to main content
The Java bindings produce type-safe classes from your compiled Daml packages. These classes let you create contracts, exercise choices, and process ledger events in Java without manually constructing gRPC messages or JSON payloads.

Code Generation

Generate Java bindings from a compiled DAR file:
This produces Java source files organized by Daml module. Each Daml template becomes a Java class, and each choice becomes a nested class with typed fields.

Generated Class Structure

For a Daml template:
The code generator produces:
  • License class — Represents the template payload with fields provider, user, description, and expiresAt
  • License.ContractId class — A typed contract identifier for License instances
  • License.Renew class — Represents the Renew choice argument with a newExpiry field
  • Serialization methodstoValue() and fromValue() for converting between Java objects and the Ledger API’s value representation

gRPC Client Setup

The generated classes work with the gRPC Ledger API client. Connect to a validator:

Submitting Commands

Create a contract using the generated class:
Exercise a choice:

Processing Events

When reading transaction events from the Ledger API stream, use fromValue() to deserialize contract payloads:

Maven / Gradle Dependency

Add the generated code to your build. In the cn-quickstart project, Gradle handles code generation and compilation as part of the build process. For a Gradle project, add the generated source directory:
For Maven, add the generated directory as a source folder in your pom.xml:
You will also need the Daml Java bindings library as a dependency. Refer to the Daml SDK GitHub releases for the current Maven coordinates and version.

Reference Documentation

The full Javadoc for the Daml Java bindings library is published with each Daml SDK release. The generated classes follow the same patterns documented there.

Daml Codegen for Java

Use the Daml Codegen for Java (dpm codegen-java) to generate Java classes representing all Daml data types defined in a Daml Archive (.dar) file. These classes simplify constructing the types required by the Java gRPC bindings for the gRPC Ledger API; for example, com.daml.ledger.api.v2.CreateCommand and com.daml.ledger.api.v2.ExerciseCommand. They also provide JSON decoding utilities, making it easier to work with JSON when using the JSON Ledger API. See How to work with contracts and transactions in Java for details on how to use the generated classes. See the sections below for guidance on setting up and invoking the codegen.

Install

Install the Daml Codegen for Java by installing DPM.

Configure

To configure the Daml Codegen, choose one of the two following methods:
  • Command line configuration: Specify all settings directly in the command line.
  • Project file configuration: Define all settings in the daml.yaml file.

Command line configuration

To view all available command line configuration options for Daml Codegen for Java, run dpm codegen-java --help in your terminal:

Project file configuration

Specify the above settings in the codegen element of the Daml project file daml.yaml. Here is an example: sdk-version: 3.4.9 name: quickstart source: daml init-script: Main:initialize parties:
  • Alice
  • Bob
  • USD_Bank
  • EUR_Bank version: 0.0.1 exposed-modules:
  • Main dependencies:
  • daml-prim
  • daml-stdlib codegen: java: package-prefix: com.daml.quickstart.iou output-directory: java-codegen/src/main/java verbosity: 2

Operate

Run the Daml Codegen using project file configuration with: $ dpm codegen-java or using command line configuration with: $ dpm codegen-java ./.daml/dist/quickstart-0.0.1.dar=com.daml.quickstart.iou —output-directory=java-codegen/src/main/java —verbosity=2

References

Generated Java code

Daml primitives to Java types

Daml built-in types are translated to the following equivalent types in Java:
Daml typeJava typeJava Bindings Value type
Intjava.lang.LongInt64
Numericjava.math.BigDecimalNumeric
Textjava.lang.StringText
Booljava.util.BooleanBool
Partyjava.lang.StringParty
Datejava.time.LocalDateDate
Timejava.time.InstantTimestamp
List or []java.util.ListDamlList
TextMapjava.util.Map Restricted to using String keys.DamlTextMap
Optionaljava.util.OptionalDamlOptional
() (Unit)None since the Java language does not have a direct equivalent of Daml’s Unit type (), the generated code uses the Java Bindings value type.Unit
ContractIdFields of type ContractId X refer to the generated ContractId class of the respective template X.ContractId

Escaping rules

To avoid clashes with Java keywords, the Daml Codegen applies escaping rules to the following Daml identifiers:
  • Type names (except the already mapped built-in types)
  • Constructor names
  • Type parameters
  • Module names
  • Field names
If any of these identifiers match one of the Java reserved keywords, the Daml Codegen appends a dollar sign $ to the name. For example, a field with the name import will be generated as a Java field with the name import$.

Generated classes

Every user-defined data type in Daml (template, record, and variant) is represented by one or more Java classes as described in this section. The Java package for the generated classes is the equivalent of the lowercase Daml module name.
A Java file that defines the class for the type Person is generated:
A Java file that defines the class for the type Name is generated:
Templates
The Daml Codegen generates the following classes for a Daml template:
TemplateName Represents the contract data or the template fields. TemplateName.ContractId Used whenever a contract ID of the corresponding template is used in another template or record, for example: data Foo = Foo (ContractId Bar). This class also provides methods to generate an ExerciseCommand for each choice that can be sent to the ledger with the Java Bindings. TemplateName.Contract Represents an actual contract on the ledger. It contains a field for the contract ID (of type TemplateName.ContractId) and a field for the template data (of type TemplateName). With the static method TemplateName.Contract.fromCreatedEvent, you can deserialize a CreatedEvent to an instance of TemplateName.Contract.
In particular, the codegen generates a file that defines six Java classes and one interface:
  1. Bar
  2. Bar.ContractId
  3. Bar.Contract
  4. Bar.CreateAnd
  5. Bar.JsonDecoder$
  6. Bar.Exercises
Variants (a.k.a. sum types)
A variant or sum type is a type with multiple constructors, where each constructor wraps a value of another type. The generated code is comprised of an abstract class for the variant type itself and a subclass thereof for each constructor. Classes for variant constructors are similar to classes for records.
The Java code generated for this variant is:
Enums
An enum type is a simplified sum type with multiple constructors but without argument nor type parameters. The generated code is standard java Enum whose constants map enum type constructors.
The Java code generated for this variant is:
Parameterized types
This section is only included for completeness. The fromValue and toValue methods would typically come from a template that does not have any unbound type parameters.
The Daml Codegen uses Java Generic types to represent Daml parameterized types. This Daml fragment defines the parameterized type Attribute, used by the BookAttribute type for modeling the characteristics of the book:
The Daml Codegen generates a Java file with a generic class for the Attribute a data type:
Convert a value of a generated type to a Java Bindings Value
To convert an instance of the generic type Attribute to a Java Bindings Value, call the toValue method and pass a function as the toValuea argument for converting the field of type a to the respective Java Bindings Value. The name of the parameter consists of toValue and the name of the type parameter, in this case a, to form the name toValuea. Below is a Java fragment that converts an attribute with a java.lang.Long value to the Java Bindings representation using the method reference Int64::new.
See Daml To Java Type Mapping for an overview of the Java Bindings Value types.
If the Daml type is a record or variant with more than one type parameter, you need to pass a conversion function to the toValue method for each type parameter.
Create a Value of a generated type from a Java Bindings Value
Analogous to the toValue method, to create a value of a generated type, call the method fromValue and pass conversion functions from a Java Bindings Value type to the expected Java type.
See Java Bindings Value class for the methods to transform the Java Bindings types into corresponding Java types.
Non-exposed parameterized types
If the parameterized type is contained in a type where the actual type is specified (as in the BookAttributes type above), then the conversion methods of the enclosing type provides the required conversion function parameters automatically.
Convert optional values
The conversion of the Java Optional requires two steps. The Optional must be mapped in order to convert its contains before to be passed to DamlOptional::of function.
To convert back DamlOptional to Java Optional, one must use the containers method toOptional. This method expects a function to convert back the value possibly contains in the container.
Convert collection values
DamlCollectors provides collectors to converted Java collection containers such as List and Map to DamlValues in one pass. The builders for those collectors require functions to convert the element of the container.
To convert back Daml containers to Java ones, one must use the containers methods toList or toMap. Those methods expect functions to convert back the container’s entries.
Daml interfaces
From this Daml definition:
The generated file for the interface definition can be seen below. Effectively it is a class that contains only the inner type ContractId because one will always only be able to deal with Interfaces via their ContractId.
For templates the code generation will be slightly different if a template implements interfaces. To allow converting the ContractId of a template to an interface ContractId, an additional conversion method called toInterface is generated. An unsafeFromInterface is also generated to make the unchecked conversion in the other direction.