Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Loading files (images) from a JAR  (Read 1893 times)
0 Members and 1 Guest are viewing this topic.
Offline crazyc94

Senior Newbie




I like cheese


« Posted 2006-01-11 04:48:56 »

Okay, here goes:

I'm trying to load two images from a .jar file that contains two folders (and the META-INF, but that doesn't matter in this situation), volatileimagetest (where the code is) and images (where the two images are).
When I run the code (below) before it's packaged into a .jar, it runs just fine.  I have two directories, volatileimagetest (where the code is) and images (where the two images are).  The directories are exactly the same as in the .jar, but not in a jar file.

Now I saw on http://weblogs.java.net/blog/chet/archive/2004/08/toolkitbuffered.html that to load images from within a .jar file, I should use the code (slightly modified of course):
1  
2  
URL url = getClass().getResource("/images/img1.png");
BufferedImage img = ImageIO.read(url);


This loads the images when running outside the .jar file.  I printed url to System.out, and got this: "file:/C:/Documents%20and%20Settings/Owner/My%20Documents/Java/VolatileImage/build/classes/images/img1.png".  I feel I should just say the directory volatileimagetest is located in the same directory as images, and contains all class files.

When I run this as a .jar, I get a NullPointException, and I narrowed it down to the line "URL url = getClass().getResource("/images/img1.png");", with getResource() returning null.

Has anyone experienced this, and if so could they please help  Huh

Quack!  I'm a duck!

"Or make your own RationalNumber class (unless you need irrational numbers, then you're screwed )." - shmoove
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #1 - Posted 2006-01-11 04:57:54 »

Can you do me a favor? Type "jar -tvf <jar filename>" on the command line and paste the output here. I'd like to see precisely how the JAR is organized. Also, what package is the main class in?

Java Game Console Project
Last Journal Entry: 12/17/04
Offline crazyc94

Senior Newbie




I like cheese


« Reply #2 - Posted 2006-01-11 05:06:08 »

I got:

     0 Tue Jan 10 21:07:06 GMT-07:00 2006 META-INF/
   202 Tue Jan 10 21:07:04 GMT-07:00 2006 META-INF/MANIFEST.MF
     0 Tue Jan 10 20:43:00 GMT-07:00 2006 images/
     0 Tue Jan 10 20:43:00 GMT-07:00 2006 volatileimagetest/
 78425 Mon Jan 09 17:05:24 GMT-07:00 2006 images/img1.PNG
   238 Mon Jan 09 17:06:06 GMT-07:00 2006 images/img2.PNG
  4717 Tue Jan 10 21:07:06 GMT-07:00 2006 volatileimagetest/Form.class
   771 Tue Jan 10 21:07:06 GMT-07:00 2006 volatileimagetest/Main.class

Quack!  I'm a duck!

"Or make your own RationalNumber class (unless you need irrational numbers, then you're screwed )." - shmoove
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #3 - Posted 2006-01-11 07:08:41 »

Well, "PNG" != "png".

Always use lowercase extensions.

弾幕 ☆ @mahonnaiseblog
Offline JuddMan

Senior Member


Medals: 1


Your Ad Here


« Reply #4 - Posted 2006-01-11 15:41:04 »

looks like you are using Windows which is not case sensitive... or maybe a mac... but java is generally case sensitive.
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #5 - Posted 2006-01-11 15:45:29 »

Well, the filesystem is case insensitive on windows (sorta), but jars certainly aren't (because the OS isn't involved in the resolving process).

弾幕 ☆ @mahonnaiseblog
Offline Grand Poeba

Junior Member





« Reply #6 - Posted 2006-01-11 16:34:30 »

is have another etra  question
lets say is hava a main jar
in which i have an images.jar which contains all my images
how can i load those?
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #7 - Posted 2006-01-11 16:59:06 »

No, you dont pack jars inside jars. (That doesnt make any sense)

Well, anyways... you could load a jar inside a jar inside a jar inside a jar... with some ZipInputStream or a JarInputStream, but it doesnt make any sense... mind you.

弾幕 ☆ @mahonnaiseblog
Offline Grand Poeba

Junior Member





« Reply #8 - Posted 2006-01-11 17:20:05 »

No, you dont pack jars inside jars. (That doesnt make any sense)

Well, anyways... you could load a jar inside a jar inside a jar inside a jar... with some ZipInputStream or a JarInputStream, but it doesnt make any sense... mind you.

to keep it cleen?
was just a question anyway
Offline crazyc94

Senior Newbie




I like cheese


« Reply #9 - Posted 2006-01-11 21:00:43 »

Well, "PNG" != "png".

Always use lowercase extensions.

I usually do.  Must have been a fluke with the image editing/creation program I was using (it defaults to upper.  I usually remember to change it to lower).  Thanks for pointing that out.  I am running Windows, so I'm used to case-insensitivity.  I'll try changing that when I get to my compiler (at school right now...).

Quack!  I'm a duck!

"Or make your own RationalNumber class (unless you need irrational numbers, then you're screwed )." - shmoove
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #10 - Posted 2006-01-12 08:08:18 »

>to keep it cleen?

Well, you can either use several jars for different parts (nice for webstart). Or if it needs to be a single jar you can just use directories (inside the jar).

弾幕 ☆ @mahonnaiseblog
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (7 views)
2014-07-29 12:53:52

Dwinin (7 views)
2014-07-29 10:59:34

E.R. Fleming (23 views)
2014-07-29 03:07:13

E.R. Fleming (9 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21

Zero Volt (51 views)
2014-07-17 23:47:54
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!