Add to your project (Maven)

Embed Notch as a library on the JVM

This page is for embedding Notch as a library inside your own program. If you only want to write .notch files and run them yourself use the command-line instead.

Requirements

Notch 0.1.3 is compiled for Java 25. Your project must build and run on Java 25 or later.

Add the dependency

Notch is published to Maven Central. Use whichever build tool your project already uses. The published version may be newer than 0.1.3. Best to use the latest stable release.

Maven

<dependency>
  <groupId>edu.montana.cs.notch</groupId>
  <artifactId>notch</artifactId>
  <version>0.1.3</version>
</dependency>

Gradle

Make sure mavenCentral() is in your repositories.

// build.gradle (Groovy DSL)
repositories { mavenCentral() }

dependencies {
  implementation 'edu.montana.cs.notch:notch:0.1.3'
}
// build.gradle.kts (Kotlin DSL)
repositories { mavenCentral() }

dependencies {
  implementation("edu.montana.cs.notch:notch:0.1.3")
}

Run Notch from Java

Whether you need a value or a program will determine how you run the script.

A value: Notch.eval

Notch.eval runs a single expression and hands the result back as a Java object.

import edu.montana.notch.Notch;

Object v = Notch.eval("1 + 2");
int    n = Notch.eval("1 + 2", Integer.class);
  • Notch.eval(String) evaluates one expression and returns the result.
  • Notch.eval(String, Class<T>) coerces the result to the requested type, or throws if no coercion applies.

To render a template, use the template engine NotchTemplates. See Notch Templates.

A whole program: Notch.run

When the script is a multiply statement program (assignments, loops, print side effects) rather than a single expression, use Notch.run. It tokenizes, parses, and executes the program against a fresh runtime.

import edu.montana.notch.Notch;

Notch.run("x = 1\nprint(x + 1)");

The program is just source text, so it can come from anywhere. Read it from a .notch file on disk rather than inlining it:

import edu.montana.notch.Notch;
import java.nio.file.Files;
import java.nio.file.Path;

Notch.run(Files.readString(Path.of("app.notch")));

Notch.run throws if the program fails to parse.

Rule of thumb:

  • eval when you want a value back.
  • run when you want to run a program.

Logging

Notch logs through SLF4J, and the Notch jar includes its own SLF4J provider (NotchLoggingServiceProvider). The template engine logs an INFO line before and after each renderTemplate call.

If your project also has an SLF4J provider, such as slf4j-simple or Logback, SLF4J prints a Class path contains multiple SLF4J providers warning. It then uses the first provider on the classpath. To select your provider, set the slf4j.provider system property:

java -Dslf4j.provider=org.slf4j.simple.SimpleServiceProvider ...

Keeping the version current

The 0.1.3 above tracks pom.xml; bump it to match the release you depend on. The pom is the source of truth for the current version.

See also