Lindera Java

Lindera Java is the Java binding of the Lindera morphological analysis engine: the artifact dev.lindera:lindera on Maven Central, whose package dev.lindera calls Lindera through a native library of its own, with JNI. Java, Kotlin and the other languages of the JVM use it.

[!NOTE] The binding is being built (Issue 1542), and is experimental. It has, so far, the loading of its native library, the versions and the features of the library, and the exceptions; tokenizing, dictionaries, the metadata and the schema, the building of dictionaries and training come next, as in Lindera .NET.

The jar and the native library

The jar has the native library of the binding for each platform, in dev/lindera/native/<os>-<arch>/, and loads the one of the machine the program runs on when the program first uses Lindera (Installation). The native library is built from the crate lindera-java of the repository, a JNI layer over the same Rust code as the other bindings; it does not go through the C API.

The jar has the version of Lindera, and has the native library of that version. It needs Java 21 or later.

How the binding works

Loading

The first use of Lindera copies the native library of the platform out of the jar, into a directory named after its version and the SHA-256 of the library (under java.io.tmpdir, or the system property dev.lindera.library.dir), and loads it with System.load. A copy that is there already, with the same content, is loaded again: programs that start at the same time share it, and Windows, which cannot delete a library in use, does not keep a copy for each start.

On Java 24 and later, the JVM warns when a library is loaded by a module whose native access is not enabled (JEP 472). A program enables it with --enable-native-access=ALL-UNNAMED when the jar is on the class path, or --enable-native-access=dev.lindera on the module path (the jar is the automatic module dev.lindera); an executable jar can say Enable-Native-Access: ALL-UNNAMED in its manifest.

A library that cannot be loaded is an UnsatisfiedLinkError that names the library and what to check. When the native library is not of the version of the Java library, it refuses to load and says which versions they are.

Versions and features

Lindera.packageVersion() is the version of the Java library, and Lindera.version() the version of the native library the program runs with; the native library loads only when they are the same.

Lindera.hasFeature tells whether the native library was built with a feature ("train", "dependency", "embed-ipadic" and so on).

import dev.lindera.Lindera;

System.out.println(Lindera.version());
System.out.println(Lindera.hasFeature("train"));

Errors

A failure of the library is a LinderaException, an unchecked exception with the kind of the failure (kind()) and the message of the library (getMessage()):

KindMeaning
INVALID_ARGUMENTAn argument is not valid
DICTIONARYA dictionary could not be found, loaded or resolved
BUILDA tokenizer or another component could not be built
IOAn input or output operation failed
PARSEData could not be parsed or decoded
VALIDATIONA schema or a record is not valid
RUNTIMEA failure of no other kind, and a feature the library was built without
PANICThe library panicked, which is a bug of the library
INCOMPATIBLE_LIBRARYThe library that was loaded is not the one of the Java library

What Java can tell before it calls the library throws the exceptions of Java: NullPointerException for a null argument, IllegalArgumentException for a string with a lone surrogate (which has no UTF-8), and IllegalStateException for an object that is closed.

Null

The package is @NullMarked (JSpecify): a type is non-null unless it is annotated @Nullable. Kotlin reads the annotations, so a String of the library is a String of Kotlin, not a String!.

Documentation

  • Installation -- Adding the jar, the platforms, and a library of one's own