Skip to content

Getting files out of a JAR or loading a shared library is difficult. We made it easy.

License

Notifications You must be signed in to change notification settings

terl/resource-loader

Repository files navigation

Resource Loader

Resource Loader gives you the functions to load resource files even if you are loading from inside a JARs or outside a JAR. Resource Loader also supports loading shared libraries through SharedLibraryLoader.

Checks

Installation

This is how to install the library in Gradle based projects.

// Top-level build.gradle
repositories {
    // ...
    mavenCentral()
}

dependencies {
    // ...
    implementation 'com.goterl:resource-loader:2.0.2' // Add this line
}

Usage

Loading a file

Let's say you have a file in your resources folder called test1.txt. To get it you simply do the following:

File file = FileLoader.get().load("test1.txt");

Resource Loader can also load from paths and directories. Just make sure the top level directory name is somewhat unique:

// The following loads test1.txt from the resources folder
// even if you supply a nested path.
File file2 = FileLoader.get().load("a_unique_top_level_folder/path/test1.txt");

// You can even load whole directories.
File dir = FileLoader.get().load("a_unique_top_level_folder/directory"); 

Loading a shared library

Loading a shared library is just as simple. You can load one by using loadSystemLibrary which loads a shared library if it is already installed on the system.

// Load from the system
SharedLibraryLoader.get().loadSystemLibrary("hydrogen", Hydrogen.class);

Or you can use load which loads a shared library even if it is bundled inside a JAR or not.

// Load from the system
SharedLibraryLoader.get().load("hydrogen", Hydrogen.class);

What problem does Resource Loader solve?

Consider the scenario. You have a project with some files in the resource folder. You're loading those files using getResourceAsStream and it's working when you test it locally. But when you go to package the project as a JAR and then run it, it fails!

Resource Loader provides developers with a fool-proof way of loading files inside or outside JARs by loading the files in a temporary folder that is deleted when the corresponding Java process that created that temp process has been destroyed.

Here are some features of Resource Loader:

  • Can be used in Android and Java projects.
  • Load from nested JARs up to 20 levels deep.
  • Resource Loader works with shared libraries (.dll, .so, .dylib) and can initialise them.
  • Can extract regular files from the resources folder.
  • Simple include this library and use the .load functions.

About

Getting files out of a JAR or loading a shared library is difficult. We made it easy.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages