• 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

Question Using getClassLoader().getResource() With an Executable Jar

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have an application that uses some image files. When I hard-code the location to the image files using:


Everything is fine, and I can make an runnable/executable jar.

But when I change the image code to:


It still runs in Eclipse, but my runnable/executable jar does nothing when I double-click it.

An ideas why that image code would make a difference between the jar executing and not executing?

Thanks in advance,
-Russ
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you show the internal structure of the jar?
 
Russ Russell
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I'm not sure how to do that. Could you explain?
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A jar file is a zip file so you can view its content with a program like WinRar.
 
Russ Russell
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, you mean just tell you the files in the jar? Okay but that one line of code shouldn't change which files are in the jar.

I'll be able to post that later tonight when I get back to my desk.

Thanks!
Russ
 
Russ Russell
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, here is the structure/contents of my jar:

META-INF/
MANIFEST.MF

(Contents of MAINIFEST.MF):
Manifest-Version: 1.0
Class-Path: .
Main-Class: com.russ.GameApp

com/russ/
Board$Clicker.class
Board$Mover.class
Board.class
GameApp.class
Thingee.class


I notice that the image files are not listed, but would that prevent the program from executing? I would think the program would still run - only the image files just wouldn't show. That has been my experience in the past. Nevertheless, why would would the jar crash when using:

but not when using:


Thank you,
-Russ S.
 
Russ Russell
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the accidental duplicate post.
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A jar file has files in a directory structure; you can think of it as a subtree of some part of a disk. So when someone wants "the structure", they mean more than the files, they want to know in what directories they are within the jar.

You have a string of method calls followed by periods; if any of the method calls returns a null, the line will fail with a null pointer exception.

Java programs don't usually just "not execute"; assuming you have console output somewhere, or can get it, there's an error message and a stack trace that is full of useful information. On a Windows machine, you can run your program from a command prompt if you don't have any other way to get a console.

So what error is encountered?

rc
 
Russ Russell
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the tip, Ralph. It's a swing/JFrame program, so I didn't even think of running it from the command line.

When I do run it from the command line I get this error:
C:\>java RussGame.jar
Exception in thread "main" java.lang.NoClassDefFoundError: RussGame/jar
Caused by: java.lang.ClassNotFoundException: RussGame.jar
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)

Can you help me decipher this? It seem to me the error is saying that there is no such file called RussGame.jar in my root C: drive (but there IS). I am using this command: java RussGame.jar

Thanks in advance for your help!
-Russ S.
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're going to run the jar, tell java:

java -jar RussGame.jar

What the message is actually telling you is that it cannot find a class named jar in the package RussGame, which is quite reasonable when put that way...
 
Russ Russell
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ralph. When I run the program from the command line the right way, I get this error:

C:\>java -jar RussGame.jar
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at com.russ.Board.<init>(Board.java:28)
at com.russ.GameApp.main(GameApp.java:8)

Hmm, looks like it can't find my image, am I correct? I'm still surprised this would crash the program, but I suppose it did. If this is the case, what am I doing wrong? How do I tell my prgram to use the picture file without hard-coding the location?

Thank you for helping me narrow this down,
-Russ S.
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've not used getResource() with getClassLoader(); I don't know the correct usage of that.

I've used getClass() and just <Classname>.class with getResource(), but it's been a while. As I remember it, the resource (image file, in this case) needs to be in the directory corresponding to the package of the class. So if the class is in the package russgames.util, then the file gotten as a resource needs to be in \russgames\util within the jar.

I've also just used ImageIcon icon = new ImageIcon("images\GreenDot.gif"), and put the images in the images directory off the root in the jar (if it were going to be multi-platform, I'd probably need to finesse the directory separator);

Hope that helps.

rc
 
Russ Russell
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. I'm pretty sure I was careful to place the image in the directory corresponding to the package, but I will check it again when I get back to work tomorrow. Does the fact that the image file is not listed in the directory listing of my jar verify that I didn't place the image in the proper location?

The funny thing is that the program ran fine in Eclipse. Perhaps Eclipse is a little more forgiving than the JVM.

Thanks again!
-Russ S.
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What it verifies is that you didn't put the image in the jar at all! I haven't created a jar in eclipse for a while, either, but I vaguely remember it automates some things for you but you have to explicitly put some other things in place yourself. You probably would not like it if it assumed all non-class files went into the jar anyway, so those you probably have to put in explicitly.

Let us know how it goes.

rc
 
Ranch Hand
Posts: 153
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry wrongly posted
 
Russ Russell
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Resolved.

Okay, here's the answer.
Eclipse needs the resource files to reside in two places. One place so that the IDE can use them, and in another place so that they can be included in the jar. Placing them in just one of these locations will not take care of the other problem. They need to reside in both places.

The two places are:
1) The root of the bin directory (NOT inside of a sub-directory there)
2) In a sub-directory under the project folder.

Eclipse has an easy way to add the sub-directory for location #2. Right click the project and select New > Source Folder.

As long as the files live in both places, the IDE can use them AND they will be included in the jar. Thank you to Ralph Cook and Wouter Oet for helping me narrow down this problem.


 
Are you here to take over the surface world? Because this tiny ad will stop you!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic