Reputation: 441
A java program uses different files, stored under the path "src/main/resources/files/files.drl". If I export this program as a run-able JAR and run this JAR, will it look for these files to be inside the JAR or it will look to be outside in a folder src->main->resources->files ?
A sample code how these files are used the Java program is :
drlFileName = "./src/main/resources/files/files.drl";
FileInputStream fis = null;
fis = new FileInputStream(drlFileName);
kbuilder.add( ResourceFactory.newInputStreamResource(fis), ResourceType.DRL);
Upvotes: 0
Views: 854
Reputation: 2709
How you package your application doesn't determine where it will find its resources in most cases. It will look in whatever directory you tell it to look in. If you specify a jar file, by using the various jar file APIs (e.g. java.util.jar.JarFile
), then that's where your program will find its data. If you look in your OS's file system (e.g. using things like java.io.FileInputStream
) then that's where it will find its data.
The exception to this is when you use ClassLoader::getResource
which will search your classpath to find the specified resource. So, for example, if your classpath points to a single jar file, then the resource will be looked for only in that jar file. Note that if you use a custom ClassLoader implementation then other places can be searched -- URLs, non-classpath paths/jarfiles, etc. Essentially any possible source of bytes could be used by a custom ClassLoader to find resources -- it will depend on what that ClassLoader was designed to do.
Upvotes: 1