Script Directives Reference

JBang uses special comments (directives) starting with // to configure how your script is compiled and executed. This reference covers all available directives.

Syntax Rules

All directives follow these rules:

  • Must start at the beginning of a line with //

  • Must be in the first comment block of the file (before any code)

  • Are case-sensitive

  • Can appear multiple times (where applicable)

  • Must have no space between // and the directive name

///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS com.example:library:1.0.0
//JAVA 17+
//PREVIEW

class MyScript {
    // ... your code
}

Dependencies and Repositories

//DEPS

Declares Maven dependencies for your script. Permits to list all the libraries dependencies used by the script. To specify dependencies you use gradle-style locators or links to Git sources. In case the dependencies needs to refer a Git repository, insert the links to those projects hosted on GitHub, GitLab or BitBucket and will then be converted to artifacts references on Jitpack. For a details description check out Dependencies.

Syntax: //DEPS groupId:artifactId:version[:classifier][@type]

Examples:

// Basic dependency
//DEPS info.picocli:picocli:4.6.3

// Multiple dependencies
//DEPS com.fasterxml.jackson.core:jackson-core:2.15.2
//DEPS com.fasterxml.jackson.core:jackson-databind:2.15.2

// With classifier
//DEPS io.netty:netty-transport-native-kqueue:4.1.107.Final:osx-aarch_64

// Fatjar dependency (excludes transitive dependencies)
//DEPS eu.maveniverse.maven.plugins:toolbox:0.1.9:cli@fatjar

// BOM POM for version management
//DEPS io.quarkus:quarkus-bom:3.2.0@pom
//DEPS io.quarkus:quarkus-core

Notes:

  • First @pom dependency is used for managed dependencies

  • Transitive dependencies are resolved automatically

  • Use @fatjar to exclude transitive dependency resolution

//REPOS

Specifies additional Maven repositories. By default, JBang uses Maven Central to resolve the dependencies listed by DEPS directive. With this directive instead, the script can override that behaviour listing custom repositories, for more detail: dependencies.adoc#repositories.

Syntax: //REPOS [name=]url[,name=url…​]

Examples:

// Add custom repository
//REPOS https://repo.custom.com/maven2

// Multiple repositories with names
//REPOS central,jcenter,custom=https://repo.custom.com/maven2

// Common repository shortcuts
//REPOS central,google,jitpack

Built-in shortcuts:

  • central - Maven Central

  • google - Google Maven Repository

  • jitpack - JitPack.io

Notes:

  • If any //REPOS is specified, Maven Central must be explicitly included

  • Repositories are searched in the order specified

  • Authentication configured via ~/.m2/settings.xml

Java Version and Features

//JAVA

Specifies the Java version requirement.

Syntax: //JAVA version[+]

Examples:

// Exact version
//JAVA 17

// Minimum version
//JAVA 11+

// For preview features
//JAVA 21+
//PREVIEW

Notes:

  • Without +, requires exact version

  • With +, specifies minimum version

  • Use with //PREVIEW for preview features

//PREVIEW

This is a switch directive that permit to enable preview features available on JDK. For example, Java record feature was available in Java since JDK 14 but in preview mode, to enable such feature use the PREVIEW directive if running on JDK where there are some features in preview mode running.adoc#preview.

Syntax: //PREVIEW

Example:

//JAVA 21+
//PREVIEW

public class RecordExample {
    // Using preview features
    record Point(int x, int y) {}

    public static void main(String[] args) {
        var p = new Point(2, 4);
        System.out.println(p);
    }
}

Notes:

  • Automatically adds --enable-preview to compile and runtime options

  • Requires compatible Java version

Compilation and Runtime Options

//COMPILE_OPTIONS

Specifies Java compiler options.

Syntax: //COMPILE_OPTIONS option1 option2 …​

Examples:

// Enable preview features manually
//COMPILE_OPTIONS --enable-preview -source 17

// Compiler warnings
//COMPILE_OPTIONS -Xlint:unchecked -Xlint:deprecation

// Optimization
//COMPILE_OPTIONS -O -g:none

//RUNTIME_OPTIONS

Specifies JVM runtime options.

Syntax: //RUNTIME_OPTIONS option1 option2 …​

Examples:

// Memory settings
//RUNTIME_OPTIONS -Xmx2g -Xms512m

// Garbage collection
//RUNTIME_OPTIONS -XX:+UseG1GC -XX:MaxGCPauseMillis=200

// System properties
//RUNTIME_OPTIONS -Dfile.encoding=UTF-8 -Duser.timezone=UTC

// Debug settings
//RUNTIME_OPTIONS -XX:+PrintGCDetails -XX:+PrintCommandLineFlags

// Performance optimization
//RUNTIME_OPTIONS -XX:+TieredCompilation -XX:TieredStopAtLevel=1

//NATIVE_OPTIONS

Specifies GraalVM native-image options.

Syntax: //NATIVE_OPTIONS option1 option2 …​

Examples:

// Basic native image optimization
//NATIVE_OPTIONS -O2 --no-fallback

// Reflection configuration
//NATIVE_OPTIONS -H:ReflectionConfigurationFiles=reflection-config.json

// Resource inclusion
//NATIVE_OPTIONS -H:IncludeResources=.*\.properties

// Advanced options
//NATIVE_OPTIONS --gc=G1 -H:+UnlockExperimentalVMOptions

// Static executable
//NATIVE_OPTIONS -H:+StaticExecutableWithDynamicLibC

//GAV

Specifies the Maven coordinates (Group:Artifact:Version) for the script itself. Useful for publishing or referencing the script as a dependency. Used by the export command in generating Maven or Gradle projects.

Syntax: //GAV groupId:artifactId[:version]

Example:

//GAV com.example:my-script:1.0.0

Notes: - If version is omitted, a default version is used. - Only one //GAV line should be present per script.

//JAVAC_OPTIONS

Specifies additional options to pass to the Java compiler (javac). Useful for advanced compilation flags.

Syntax: //JAVAC_OPTIONS option1 option2 …​

Example:

//JAVAC_OPTIONS -parameters -Xlint:unchecked

Notes: - Options are passed directly to javac. - Use for flags not covered by //COMPILE_OPTIONS.

Application Configuration

//MAIN

Used in scripts to override the default entry point, specifying a different main class result as a permanent modification and will stored also in the generated jar. For more details consult running.adoc#setting-main-class.

Syntax: //MAIN fully.qualified.ClassName

Example:

//MAIN com.example.AlternativeMain

class Primary {
    public static void main(String[] args) {
        System.out.println("Primary main");
    }
}

class AlternativeMain {
    public static void main(String[] args) {
        System.out.println("Alternative main - this will run");
    }
}

Notes:

  • Overrides automatic main class detection

  • Useful when multiple main methods exist

//MODULE (EXPERIMENTAL)

The module directive let the code to be built as a Java module. Can be used as command line switch as in running.adoc#module-support-experimental.

Declares the script as a Java module.

Syntax: //MODULE module.name

Example:

//MODULE com.example.myapp

package com.example.myapp;

public class ModularApp {
    public static void main(String[] args) {
        System.out.println("Running as module: com.example.myapp");
    }
}

Notes:

  • Enables module system compilation

  • Dependencies automatically marked as required

  • Requires package declaration

//MANIFEST

Let you specify the manifest file key-values in the generated jar file running.adoc#adding-entries-to-manifest-mf.

Syntax: //MANIFEST key=value key2=value2 …​

Examples:

// Basic manifest entries
//MANIFEST Built-By=Developer Sealed=true

// Version information
//MANIFEST Implementation-Version=1.0.0 Implementation-Vendor=MyCompany

// Boolean flag (no value = true)
//MANIFEST Custom-Flag Multi-Release

Notes:

  • Entries without =value default to true

  • Useful for application metadata

Performance Optimization

//CDS (EXPERIMENTAL)

Enables Application Class Data Sharing for faster startup, available from JDK 13+ permit to enable the feature provided by the JDK running.adoc#experimental-application-class-data-sharing.

Syntax: //CDS

Example:

//CDS
//RUNTIME_OPTIONS -Xms256m

class FastStartup {
    public static void main(String[] args) {
        System.out.println("Fast startup with CDS");
    }
}

Notes:

  • Requires Java 13+

  • Improves startup time for frequently-run scripts

  • Can be overridden with --no-cds command line option

//JAVAAGENT

Specifies a Java agent configuration options directly from the script running.adoc#java-agents.

Syntax: //JAVAAGENT [path|gav|url][=options]

Examples:

// Mark as agent (for building agents)
//JAVAAGENT

// Use external agent
//JAVAAGENT io.opentelemetry.javaagent:opentelemetry-javaagent:1.20.0

// Local agent with options
//JAVAAGENT myagent.jar=option1,option2

// Remote agent
//JAVAAGENT https://repo1.maven.org/maven2/agent.jar

Notes:

  • Without arguments, marks script as Java agent

  • Can reference local files, Maven coordinates, or URLs

Language-Specific Directives

//KOTLIN

Specifies Kotlin compiler version.

Syntax: //KOTLIN version

Example:

///usr/bin/env jbang "$0" "$@" ; exit $?
//KOTLIN 2.0.21
//DEPS org.jetbrains.kotlin:kotlin-stdlib:2.0.21

fun main(args: Array<String>) {
    println("Hello from Kotlin ${args.firstOrNull() ?: "World"}")
}

//GROOVY

Specifies Groovy compiler version.

Syntax: //GROOVY version

Example:

///usr/bin/env jbang "$0" "$@" ; exit $?
//GROOVY 3.0.19
//DEPS org.codehaus.groovy:groovy:3.0.19

def name = args.length > 0 ? args[0] : "World"
println "Hello from Groovy $name"

File and Resource Management

//SOURCES

Includes additional source files in compilation.

When multiple files are part of the same JBang project, this directive comes to help to list other source files than the main script that need to be processed, for more details check organizing.adoc#multiple-source-files.

Syntax: //SOURCES file1.java file2.java …​

Example:

//SOURCES utils/Helper.java model/Person.java

class MainApp {
    public static void main(String[] args) {
        Helper helper = new Helper();
        Person person = new Person("Alice");
        helper.process(person);
    }
}

Notes:

  • Files are relative to the main script location

  • All files compiled together

  • Useful for multi-file scripts

//FILES

Includes additional files in the script execution environment.

This directive let’s to embed multiple resource files, like manifests, properties files or whatelse to the jar generated from a script organizing.adoc#adding-more-resources. When a project is exported these files are inserted under resources folder exporting.adoc#exporting-as-a-project.

Syntax: //FILES file1.txt file2.properties …​

Example:

//FILES config.properties data.txt templates/

class FileProcessor {
    public static void main(String[] args) throws Exception {
        // These files are available in working directory
        Properties props = new Properties();
        props.load(new FileInputStream("config.properties"));

        List<String> lines = Files.readAllLines(Paths.get("data.txt"));
        System.out.println("Loaded " + lines.size() + " lines");
    }
}

Notes:

  • Files copied to script execution directory

  • Supports directories (copied recursively)

  • Paths relative to script location

Documentation and Metadata

//DESCRIPTION

Provides a short description of script functionality. This information is used by the alias catalog to help users understand which is the functionality provided by the script. For its usage in alias management check out alias_catalogs.adoc#describe-aliases.

Syntax: //DESCRIPTION text

Example:

//DESCRIPTION Database migration utility for MyApp
//DESCRIPTION Supports PostgreSQL and MySQL databases
//DEPS org.postgresql:postgresql:42.6.0

class DbMigrate {
    public static void main(String[] args) {
        System.out.println("Running database migration...");
    }
}

Notes:

  • Multiple //DESCRIPTION lines are concatenated

  • Used by jbang alias list and jbang app list

  • Helps document script purpose

//DOCS

Links to additional documentation resources for the script.

When the script has its own reference guide published on the internet, or a link to a maven repository is needed the DOCS directive permit to define multiple links that are displayed as information of the script itself. Each link can have a non unique taa that would help to group them. So for example if there is a DOCS directive referring to a local file and an URL which points to a site both referring to some form of documentation for the script, then both can be tagged as guide, like in the snippet:

//DOCS guide=./readme.md
//DOCS guide=http://www.jbang.dev/documentation/guide/latest/index.html

In case no tag is provided then they all fall into the main generic tag.

This information is displayed as result of the info docs which can refer both to a script or an alias. When the script is added as an alias to a catalog, the user can decide to override the DOCS directives, specifying other URLs or file paths, like in:

jbang alias add --docs https://www.jbang.dev/documentation/guide/latest/javaversions.html itests/docsrun.java

The --docs CLI argument can accept a list of comma separated references to override the DOCS directives present in the script

Syntax: //DOCS url-or-path

Example:

//DOCS https://myproject.org/docs/usage.html
//DOCS docs/extra-info.md

Notes: - Can be used multiple times to link to several resources. - Used by tools to provide context/help for scripts.

Advanced Usage Patterns

Complex Application

///usr/bin/env jbang "$0" "$@" ; exit $?
//DESCRIPTION Production microservice with monitoring and metrics
//JAVA 17+
//DEPS io.quarkus:quarkus-bom:3.2.0@pom
//DEPS io.quarkus:quarkus-resteasy-reactive
//DEPS io.quarkus:quarkus-micrometer-registry-prometheus
//REPOS central,quarkus=https://repo1.maven.org/maven2/
//RUNTIME_OPTIONS -Xmx512m -XX:+UseG1GC
//NATIVE_OPTIONS --no-fallback -H:+ReportExceptionStackTraces
//MANIFEST Implementation-Version=1.0.0 Built-By=CI

// Your application code here

Performance-Optimized Script

///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 21+
//CDS
//RUNTIME_OPTIONS -XX:+TieredCompilation -XX:TieredStopAtLevel=1
//RUNTIME_OPTIONS -Xmx256m -XX:+UseSerialGC
//COMPILE_OPTIONS -O -g:none

// Fast-starting script

Multi-Language Project

///usr/bin/env jbang "$0" "$@" ; exit $?
//SOURCES kotlin/Utils.kt groovy/Scripts.groovy
//DEPS org.jetbrains.kotlin:kotlin-stdlib:2.0.21
//DEPS org.codehaus.groovy:groovy:3.0.19
//FILES config/ templates/

// Mixed-language application

Best Practices

Directive Ordering

Recommended order for readability:

///usr/bin/env jbang "$0" "$@" ; exit $?
//DESCRIPTION Your script description
//JAVA 17+
//PREVIEW

//JAVAAGENT agent.jar
//MAIN com.example.Main
//MODULE com.example.module

//REPOS custom-repo
//DEPS dependency1
//DEPS dependency2

//SOURCES additional-files
//FILES resource-files

//MANIFEST Built-By=Developer
//COMPILE_OPTIONS -Xlint:all
//RUNTIME_OPTIONS -Xmx1g
//NATIVE_OPTIONS --no-fallback
//CDS

Environment-Specific Configuration

// Use environment variables in directives
//DEPS org.postgresql:postgresql:${env.DB_VERSION:42.6.0}
//RUNTIME_OPTIONS -Xmx${env.MAX_MEMORY:1g}

Conditional Directives

Some directives can be conditionally applied:

// Different options based on OS
//NATIVE_OPTIONS ${os.detected.name:linux}=-H:+StaticExecutableWithDynamicLibC
//DEPS org.openjfx:javafx-graphics:17.0.2:${os.detected.jfxname}

Reference Quick List

Directive Purpose Example

//DEPS

Maven dependencies

//DEPS com.example:lib:1.0

//REPOS

Additional repositories

//REPOS central,custom=https://…​

//JAVA

Java version

//JAVA 17+

//PREVIEW

Enable preview features

//PREVIEW

//COMPILE_OPTIONS

Compiler options

//COMPILE_OPTIONS -Xlint:all

//RUNTIME_OPTIONS

JVM options

//RUNTIME_OPTIONS -Xmx2g

//NATIVE_OPTIONS

Native image options

//NATIVE_OPTIONS --no-fallback

//MAIN

Main class override

//MAIN com.example.Main

//MODULE

Module declaration

//MODULE com.example.app

//MANIFEST

JAR manifest entries

//MANIFEST Built-By=Dev

//CDS

Class Data Sharing

//CDS

//JAVAAGENT

Java agent

//JAVAAGENT agent.jar

//KOTLIN

Kotlin version

//KOTLIN 2.0.21

//GROOVY

Groovy version

//GROOVY 3.0.19

//SOURCES

Additional sources

//SOURCES util/Helper.java

//FILES

Include files

//FILES config.properties

//DESCRIPTION

Script description

//DESCRIPTION My utility

What’s Next?

Master these directives to unlock the full power of JBang scripting! 🚀