I'm a developing a Java library that is going to be used internally in our company. It provides a wrapper around Aerospike. When a user calls the factory method, the library should read a file from its resources.
public static IAerospikeClient newTypedClient(IAerospikeClient client) {
LOG.info("Trying to load aerospike schema yml");
URL resource = AerospikeTypedClientFactory.class.getClassLoader().getResource("schema.yml");
if (resource == null) {
throw new IllegalArgumentException("File 'schema.yml' is not found");
}
byte[] bytes = Files.readAllBytes(Paths.get(resource.toURI()));
return new TypedAerospikeClient(client, new String(bytes));
}
Then I'm trying to add this library as a dependency and call newTypedClient
method, I get this error.
Caused by: java.nio.file.FileSystemNotFoundException
at com.sun.nio.zipfs.ZipFileSystemProvider.getFileSystem(ZipFileSystemProvider.java:171)
at com.sun.nio.zipfs.ZipFileSystemProvider.getPath(ZipFileSystemProvider.java:157)
at java.nio.file.Paths.get(Paths.java:143)
at com.example.AerospikeTypedClientFactory.newTypedClient(AerospikeTypedClientFactory.java:30)
Is there any way to overcome this error? I guess I could add the schema.yml
file to the resources
folder of the library's consumer. But I definitely don't want to go into this. Because the purpose is to get rid of configurations and put them within a single artefact.
EDIT 1
For those who refer to How should I use getResource() in Java? question. I do understand that getResource
reads from the classpath. My question is how can I get this work? Perhaps, I can replace getResource
/getResourceAsStream
usage with something else. The idea is that schema.yml
has to be packed within the library .jar
archive.
EDIT 2
Here is the problem step by step.
- The described code is put within the
lib
module. - The
lib
is packed tojar
and published to Artifactory. - The
service
put a dependency onlib
. - The
service
calls thenewTypedClient
method from thelib
. - The
FileSystemNotFoundException
raises.
So, I need lib
to read schema.yml
from the jar it is packed to. Is it possible? Here is the diagram that describes the process.