I've been searching around for hours trying to fix this problem and found multiple posts about similar problems, but nothing I find seems to do the exact thing I'm trying.
I have a res
folder at the same level of my src
folder which I use for things like images and text files. I can easily spit out the text file in Eclipse by simply doing:
FileInputStream fis = new FileInputStream("res/misc/foo.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line;
while((line = br.readLine()) != null) {
System.out.println(line);
}
However, the problem starts when I package this up in a runnable JAR file. I found multiple threads pointing this out:
Class.class.getResourceAsStream("/misc/foo.txt");
However, this only works if the resources are in the same source
folder as the class. And indeed, when i move the res
folder into src
it can find the resource, but that's not how I want to organize my project.
Is there some way to do this that I missed, or should I change my folder structure?
You should be able to get the path with something like:
String path = <YourClassName>.class.getProtectionDomain().getCodeSource().getLocation().getPath();
Then a quick test to check if the directory is there:
File folder = new File(path);
File[] dirList = folder.listFiles();
for (File list : dirList) {
if (list.isDirectory()) {
System.out.println("Directory " + list.getName());
}
}
You're going to have to change your structure, such as make res
a source folder as well (you can have more than one source folder). It's not really about being a source folder, it's about getting your resources to be copied into the same tree that makes up the contents of the resulting jar.
Just likenitind already said:
Create the folder res
as a source folder (just like src
). Put your files there and then you can access them the same way you access files in your src
ie
FileInputStream fis = new FileInputStream("/misc/foo.txt");
In Eclipse you can simply create a new file as "Source Folder". You could also just copy the scr
folder and rename it to lets say res
.
Use maven resource plugin, The resources plugin copies files from input resource directories to an output directory.
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<configuration>
...
</configuration>
</plugin>
Assume we want to copy resource files from the directory input-resources to the directory output-resources and we want to exclude all files ending with the extension .png.
just like excludes you can include files using
These requirements are satisfied with this configuration:
<configuration>
<outputDirectory>output-resources</outputDirectory>
<resources>
<resource>
<directory>input-resources</directory>
<excludes>
<exclude>*.png</exclude>
</excludes>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.