Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/terl/resource-loader
Getting files out of a JAR or loading a shared library is difficult. We made it easy.
https://github.com/terl/resource-loader
jar java java-8 library loading shared-library
Last synced: about 1 month ago
JSON representation
Getting files out of a JAR or loading a shared library is difficult. We made it easy.
- Host: GitHub
- URL: https://github.com/terl/resource-loader
- Owner: terl
- License: mit
- Created: 2019-08-10T23:22:16.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-03-18T15:47:34.000Z (8 months ago)
- Last Synced: 2024-09-29T23:05:53.895Z (about 1 month ago)
- Topics: jar, java, java-8, library, loading, shared-library
- Language: Java
- Size: 5.52 MB
- Stars: 19
- Watchers: 7
- Forks: 10
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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](https://github.com/terl/resource-loader/actions/workflows/primary.yml/badge.svg)](https://github.com/terl/resource-loader/actions/workflows/primary.yml)
## Installation
This is how to install the library in Gradle based projects.
```groovy
// 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:
```java
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:
```java
// 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.```java
// 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.
```java
// 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:
- [x] Can be used in Android _**and**_ Java projects.
- [x] Load from _nested_ JARs up to 20 levels deep.
- [x] Resource Loader works with shared libraries (`.dll`, `.so`, `.dylib`) and can initialise them.
- [x] Can extract regular files from the `resources` folder.
- [x] Simple include this library and use the `.load` functions.