0

I am making a game and have text files inside of my resources folder that I use to store unit data. While running my game from my IDE, I have no problems with the code using Googles Guava to load the files to memory. However, when I tried to package it as a jar, loading text files using the Files methods causes crashes.

I have seen that input resource stream may be a solution to properly load text file data from txt files stored inside of the jar. Could someone please show me how to use the method properly to load from input resource stream directly to a String.

Thank you

String forestString = this.forestkinFileMap.get(forestKin);
URL url = Resources.getResource(forestString);
String text = null;
try {
    text = Resources.toString(url, StandardCharsets.UTF_8);
} catch (IOException e) {
    e.printStackTrace();
}
UnitTile unit = this.unitTileFactory.processUnitCodeComposite(forestString);
return unit;

Also, this is for the client program, but since its just a matter of loading text files inside the jar, this shouldn't be a relevant factor

VGR
  • 40,506
  • 4
  • 48
  • 63
  • 1
    #1 Share us the error and how are you loading the txt. #2 This jar will be the server game or client game? – JRichardsz Nov 02 '21 at 16:03
  • This is the code that works when run from the IDE but fails when compiled to a jar. Again during the IDE launch it works perfectly. So its the reading of text files inside the jar.String forestString = this.forestkinFileMap.get(forestKin); URL url = Resources.getResource(forestString); String text = null; try { text = Resources.toString(url, StandardCharsets.UTF_8); } catch (IOException e) { e.printStackTrace(); } UnitTile unit = this.unitTileFactory.processUnitCodeComposite(forestString); return unit; – nbulgarides Nov 02 '21 at 16:06
  • add it to the question enclosed with ``` my code ... ``` and this too: #2 This jar will be the server game or client game? – JRichardsz Nov 02 '21 at 16:13
  • Please add the full stack trace of whatever exception you’re seeing, including any “Caused by” sections. – VGR Nov 02 '21 at 17:27

1 Answers1

0

The name of a resource must be an archive entry’s name.

A .jar file is actually a zip file, usually with some Java-specific zip entries in the archive. This means you can open a .jar file in any zip tool.

In Windows, you can make a copy of your .jar file, rename it so it has a .zip extension, and double-click it.

Of course, every JDK has a jar tool, so you can just use that tool to view the .jar file’s entries:

jar tf C:\path\to\program.jar | more

If you see this:

com/nbulgarides/game/data.txt

Then you have to use that path when loading a resource:

Resources.getResource("com/nbulgarides/game/data.txt")

The string argument is not a file name and you cannot just pass any file name to a getResource method. A resource path is always relative to the root of a .jar file (or, in theory, a directory that’s on the classpath, though no application is distributed to users that way). Also, resource paths always use forward slashes (/), on every platform, including Windows.

You don’t need a guava class to do this. Regular Java SE classes can do it:

String text;
try (InputStream textSource =
    MyGame.class.getResourceAsStream("/com/nbulgarides/game/data.txt")) {

    text = new String(textSource.readAllBytes(), StandardCharsets.UTF_8);
}
VGR
  • 40,506
  • 4
  • 48
  • 63
  • Thank you for your feedback. Can a program edit the text files stored inside of the jar file itself if i deploy my game to a single jar? My file pathing is routing correctly during run time in the ide but the same code doesn't correctly work once the game is in jar form. – nbulgarides Nov 03 '21 at 17:16
  • Absolutely not. Entries in a .jar file are read-only. What most applications do is use the .jar entries as *default data,* and when the user exits, the application stores a *copy* of the data in a known location. (For an example of known locations, see https://stackoverflow.com/questions/35388882/find-place-for-dedicated-application-folder.) – VGR Nov 03 '21 at 17:49