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 totrue
-
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
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
andjbang 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
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
Reference Quick List
Directive | Purpose | Example |
---|---|---|
|
Maven dependencies |
|
|
Additional repositories |
|
|
Java version |
|
|
Enable preview features |
|
|
Compiler options |
|
|
JVM options |
|
|
Native image options |
|
|
Main class override |
|
|
Module declaration |
|
|
JAR manifest entries |
|
|
Class Data Sharing |
|
|
Java agent |
|
|
Kotlin version |
|
|
Groovy version |
|
|
Additional sources |
|
|
Include files |
|
|
Script description |
|
What’s Next?
-
Apply these directives → Try them in your First Script
-
Learn about dependencies → Read the Dependencies Guide
-
Optimize performance → Check Execution Options
-
Create native images → Explore Native Images
Master these directives to unlock the full power of JBang scripting! 🚀