aspose file tools*
The moose likes Beginning Java and the fly likes Question Using getClassLoader().getResource() With an Executable Jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question Using getClassLoader().getResource() With an Executable Jar" Watch "Question Using getClassLoader().getResource() With an Executable Jar" New topic
Author

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

Russ Russell
Ranch Hand

Joined: Jan 24, 2010
Posts: 72
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
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Could you show the internal structure of the jar?


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Russ Russell
Ranch Hand

Joined: Jan 24, 2010
Posts: 72
Sorry, I'm not sure how to do that. Could you explain?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

A jar file is a zip file so you can view its content with a program like WinRar.
Russ Russell
Ranch Hand

Joined: Jan 24, 2010
Posts: 72
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

Joined: Jan 24, 2010
Posts: 72
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

Joined: Jan 24, 2010
Posts: 72
Sorry for the accidental duplicate post.
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
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

Joined: Jan 24, 2010
Posts: 72
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

Joined: May 29, 2005
Posts: 479
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

Joined: Jan 24, 2010
Posts: 72
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

Joined: May 29, 2005
Posts: 479
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

Joined: Jan 24, 2010
Posts: 72
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

Joined: May 29, 2005
Posts: 479
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
Malatesh Karabisti
Ranch Hand

Joined: Jul 28, 2010
Posts: 153

sorry wrongly posted
Russ Russell
Ranch Hand

Joined: Jan 24, 2010
Posts: 72
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.


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question Using getClassLoader().getResource() With an Executable Jar