Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

stardust-enterprises/yanl

Repository files navigation

yanl - yet another native loader

Build Maven Central

yet another native library loader and extractor for the JVM, written in Kotlin.

why

other libraries simply do not fit my needs and/or are not as expandable/configurable as I would like.

how use

you can import yanl from maven central just by adding it to your dependencies:

gradle

dependencies {
    implementation("fr.stardustenterprises:yanl:{VERSION}")
}

maven

<dependency>
    <groupId>fr.stardustenterprises</groupId>
    <artifactId>yanl</artifactId>
    <version>{VERSION}</version>
</dependency>

how work

this library can act as a replacement from your standard System.loadLibrary, with a bit more configuration setup.

consider the following example:

package your.project;

public class Program {
    public static void main(String[] args) {
        // requires the native file to be in path
        System.loadLibrary("coollib");

        // do stuff...
    }
}

this would require the library file to be somewhere on the PATH of the host machine.

one way of getting around this problem would be to extract the library from the jar archive to a known folder, then to load it. this approach works but also requires extracting and loading the correct library for the correct processor architecture (arch) and operating system (OS).

and this is where yanl performs: we take care of platform detection, library extraction and location with ease.

taking back that previous example, let us see an extensive configuration:

package your.project;

import fr.stardustenterprises.yanl.NativeLayout;
import fr.stardustenterprises.yanl.NativeLoader;
import fr.stardustenterprises.yanl.PlatformContext;
import fr.stardustenterprises.yanl.TempExtractor;

public class Program {
    private static final NativeLoader nativeLoader =
        new NativeLoader.Builder()

        // this tells the loader to look for libraries
        // in the /META-INF/natives directory
        .root("/META-INF/natives")

        // this sets where the loader is going
        // to look for your libraries. you can
        // check NativeLayout for default layouts
        .layout(NativeLayout.HIERARCHICAL_LAYOUT)

        // you can also only specify the layout's
        // pattern, and it will automagically create
        // it for you
        //.layout("{os}/{arch}/{name}")

        // set the extractor instance,
        // see the Extractor interface
        .extractor(new TempExtractor())

        // set the context instance,
        // see the Context interface
        .context(new PlatformContext())

        // create the loader
        .build();

    public static void main(String[] args) {
        // replaces System.loadLibrary
        nativeLoader.loadLibrary("coolLib");

        // do cooler stuff...
    }
}

Note: the above example is the default configuration for the NativeLoader, so you could replace the entire building process with the following:

package your.project;

import fr.stardustenterprises.yanl.NativeLoader;

public class Program {
    private static final NativeLoader defaultLoader =
        new NativeLoader.Builder().build();
}

troubleshooting

if you ever encounter any problem related to the library, you can open an issue describing what the problem is. please, be as precise as you can, so that we can help you asap. we are most likely to close the issue if it is not related to our work.

contributing

you can contribute by forking the repository, making your changes and creating a new pull request describing what you changed, why and how.

licensing

this project is under the ISC license.