2

I'm working on Java project and I have a little problem.

My project is currently runnable and works correctly when i launch him from IntelliJ. To work, he needs some resources like images or .txt files.

This is my current file configuration

My problem is:

When I export the project in executable jar file, he doesn't work. The reason is that he cannot find the resources.

However, I have used Class.class.getResourcesAsStream(String).

The most curious is that resources are in the jar, and correctly placed...

Anyone can help us ?

Thanks for your help.

My Code

File f = new File(Loader.class.getResource("/xsbfiles").getFile());
System.out.println(f.getAbsolutePath() +": exists ? " + f.exists());
System.out.println(f.getAbsolutePath());

Launching of the application from Intellij

/Users/gael/IdeaProjects/Sockoban/out/production/Sokoban/xsbfiles: exists ? true
/Users/gael/IdeaProjects/Sockoban/out/production/Sokoban/xsbfiles
Opening /Users/gael/IdeaProjects/Sockoban/out/production/Sokoban/xsbfiles/MicroCosmos.txt
Successful Opening

Launching of the jar file from terminal

/Users/gael/IdeaProjects/Sockoban/out/artifacts/Sokoban/file:/Users/gael/IdeaProjects/Sockoban/out/artifacts/Sokoban/Sokoban.jar!/xsbfiles: exists ? false
/Users/gael/IdeaProjects/Sockoban/out/artifacts/Sokoban/file:/Users/gael/IdeaProjects/Sockoban/out/artifacts/Sokoban/Sokoban.jar!/xsbfiles
No xsbfile found

Edit:

Finally, I have solve my problem using BufferedReader like in the example:

BufferedReader reader = new BufferedReader(
            new InputStreamReader(MyClass.class.getResourceAsStream("/xsbfiles/MicroCosmos.txt")));

This is a good solution to load one file. However, I need to read all files in the folder called xsbfiles...

I have searched in different sources and found a similar question.

An answer suggests to use a "hack" to list files contained in the folder. This is the example of code by Boris the Spider:

public static List<URL> getResources(final String path) throws IOException {
final ClassLoader loader = Thread.currentThread().getContextClassLoader();
try (
        final InputStream is = loader.getResourceAsStream(path);
        final InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
        final BufferedReader br = new BufferedReader(isr)) {
    return br.lines()
            .map(l -> path + "/" + l)
            .map(r -> loader.getResource(r))
            .collect(toList());
    }
}

To well understand the code, I have run him on my IDE and in the terminal. It's seems run correctly on my IDE but not with the jar.

What did I do that doesn't work ?

  • 2
    Please [edit] your question to include an [mcve] demonstrating the problem. – Slaw Dec 16 '18 at 21:10
  • I have updated the question. As you can see, the resources are not found when i have compressed my project in jar file, using intellij. This is the first time I have used this site to solve a problem. I'm sorry if I'm not being very clear. – Gael Marcadet Dec 16 '18 at 21:33
  • 1
    You're attempting to access resources with the `File` API. This won't work when packaged in a JAR file. Take a look at [Reading a resource file from within jar](https://stackoverflow.com/questions/20389255/reading-a-resource-file-from-within-jar). – Slaw Dec 16 '18 at 21:34
  • And no worries, we've all been new to Stack Overflow at one point. If you haven't already you can take the [tour] and visit the [help] for information about this site. Some of the more important articles are [ask] and [answer]. – Slaw Dec 16 '18 at 21:43
  • First of all, thanks for your help ;) I will take a moment to read the differents How-To. About this problem, I will try to fix him. – Gael Marcadet Dec 16 '18 at 21:59
  • 1
    The getFile() method of URL *does not* return a valid file name. It just returns the path portion of the URL, which will have escape sequences for the many characters which are not allowed to appear directly in URLs. Read the image or text directly from the URL and, as Slaw said, avoid using the File class. – VGR Dec 17 '18 at 01:35
  • I have fixed the problem with the BufferedReader. It's a pretty good solution to read a file. However, it's seems doesn't work to list files in folder. I have read some documentation but nothing for the moment. The function in the question returns URLs, it's seems not work for Jar. I'm continuing my research. – Gael Marcadet Dec 17 '18 at 19:41

0 Answers0