• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Text Files in a Self-Contained Jar

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello again. I managed to get all the images into the jar file using.

getClass().getResource("/" + imageName);

Now I'm trying to get the text files that are read in to build maps of areas using the images. I thought I would use the same but the File Class does not like URLs in its constructor. To solve this I appended a .getPath() to the end of the statement.

getClass().getResource("/" + textFile).getPath();

But the program does not seem to like this. I end up getting a path that looks something like the following:

/home/Users/Gad/Programming/Game/game.jar!/Maps/map.top

If I've tried removing the slash so it is:

/home/Users/Gad/Programming/Game/game.jar!Maps/map.top

As well as a number of other things to try to get this to work including removing the exclamation point but no luck on all fronts.

When the error is thrown it says approximately the following:

Cannot Find File:
File://home/Users/Gad/Programming/Game/game.jar!Maps/map.top

When I get another opportunity to work on it tonight I plain to toString() the image files that make it into the jar to see if they'll provide me a hint as to the directory structure of a jar.

Thanks for any help, sorry I posted again but no one replied to my last post in the other thread.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can get an InputStream from which to read by using the getResourceAsStream method instead of getResource.
 
Gad Flailimbs
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a ton. This worked great.

Here's a link to the self-contained game Jar if you're interested.

Game Link
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic