Results 1 to 12 of 12
Thread: using getResource
- 02-23-2013, 11:48 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 11
- Rep Power
- 0
using getResource
Guys I am going crazy on this one. I have an application that I am packaging as a jar file. It is a multiplayer chess game in Java 3D. When I package it in to a jar file it doesn't load the image. I am using ObjectFile.load(URL) to load the .obj file. when I use ObjectFile.load("resources/queen.obj"); it loads properly in eclipse. When I package it in a jar it doesn't find the file. So after some reading I see I should be using getResource to get the URL and then use ObjectFile.load(URL url);
My issue is the following: I can not figure out where to put these files to get them to load correctly. I have tried every combination of directory hierarchies I could think of and it can NEVER find the file. My current directory structure is this:
The project name is Final
Final contains 3 folders: bin, src, resources
src contains the folder: game (which is the main package)
game contains: Display.java (main class) , Position.java Piece.java
resources folder contains: queen.obj
ObjectFile.load is being called from Piece.java and I went to use a relative path because this jar will have to be portable. I realize the package naming isn't correct but that isn't my concern here unless that effects my main issue. I am willing to change the directory hierarchy, I just want this thing to find the .obj file. Does anyone have any ideas???? Thanks!
-
Re: using getResource
Where are the class files located? Consider placing the resources folder in a directory off of these guys.
- 02-24-2013, 04:08 AM #3
Member
- Join Date
- Jun 2011
- Posts
- 11
- Rep Power
- 0
Re: using getResource
I tried that. I tried placing the images directly in the class folder in case i was using a "/" the wrong way. I tried placing them in a folder in the directory the classes are on. Every time resource = null :[
-
Re: using getResource
You have not given enough information for us to be able to help you yet, I don't think. You will want to show the structure of your jar file and how you are trying to get the resource.
I suggest that you simplify and isolate the problem as much as possible to start with. Let's not work with your current program but rather a single simple Java program that resides in a package and that has been jar'd, and all it does is create an ImageIcon from an Image held in the jar which it then tries to display in a JOptionPane. Show us your complete jar structure for this, and any error messages that you might be seeing.
-
Re: using getResource
For example, a minimal program could look like so:
And then if you are using Windows, open a cmd window that holds your jar file, type in jar tf your_jar_file_name, and copy and paste what you see here. For example my file above was jar'd into a Foo.jar file, and jar tf Foo.jar printed:Java Code:import java.awt.image.BufferedImage; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JOptionPane; public class Foo { public static void main(String[] args) { String resourcePath = "resources/blackjack strategy.JPG"; try { BufferedImage bImg = ImageIO.read(Foo.class.getResource(resourcePath )); ImageIcon icon = new ImageIcon(bImg); JOptionPane.showMessageDialog(null, icon); } catch (IOException e) { e.printStackTrace(); } } }
C:\Users\Pete\Documents\Programming\Java\Jar Files>jar tf Foo.jar
META-INF/MANIFEST.MF
pkg/Foo.class
pkg/Foo.java
pkg/resources/blackjack strategy.JPG
.classpath
.project
- 02-24-2013, 05:42 AM #6
Member
- Join Date
- Jun 2011
- Posts
- 11
- Rep Power
- 0
Re: using getResource
Ok. Thank you so much for your time, I really appreciate it. I am doing this right now. Before reading this I extracted the jar file I had and realized the .obj files were not even included in the .jar file so I guess I will know the answer to this problem when I figure out how to include them correctly. I added a source folder under project properties and threw them in there and it still didn't put them in the jar file so I am doing something wrong here. I am doing what you suggested also right now. Thanks again
- 02-24-2013, 05:51 AM #7
Member
- Join Date
- Jun 2011
- Posts
- 11
- Rep Power
- 0
Re: using getResource
@bt:~/Desktop/test# ls
test.jar
#@bt:~/Desktop/test# jar xf test.jar
#@bt:~/Desktop/test# ls
game META-INF test.jar
#@bt:~/Desktop/test# cd game
#@bt:~/Desktop/test/game# ls
Foo.class
#@bt:~/Desktop/test/game# cd ~/Desktop/test/META-INF
#@bt:~/Desktop/test/META-INF# ls
MANIFEST.MF
I'm running linux, so I used the appropriate command. The only different was I stuck the main class in a package called game. Was this a bad idea? Here are the results. I created a resource directory and put the image in there too and it isn't in the jar file so I guess I need to figure out why my jar file isn't including the image. (It's not really an image its an .obj file but that is irrelevant I imagine.) I tried adding another external source folder, I tried putting them in the default directory under Project Properties Resource, still didn't show up hmm.. any ideas? Thanks again, I do really appreciate you taking your time to help me with this!
- 02-24-2013, 06:36 AM #8
Member
- Join Date
- Jun 2011
- Posts
- 11
- Rep Power
- 0
Re: using getResource
Ok. I solved the problem, thanks so much for pointing me in the right direction, I added a source folder in eclipse and dragged the files in to the eclipse source folder. I still don't know how I could have done this manually but regardless, its working. Thanks so much! now on to my next problem:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoa der.main(JarRsrcLoader.java:58)
Caused by: java.lang.UnsatisfiedLinkError: no j3dcore-ogl in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java :1681)
at java.lang.Runtime.loadLibrary0(Runtime.java:840)
at java.lang.System.loadLibrary(System.java:1047)
at javax.media.j3d.NativePipeline$1.run(NativePipelin e.java:231)
at java.security.AccessController.doPrivileged(Native Method)
at javax.media.j3d.NativePipeline.loadLibrary(NativeP ipeline.java:200)
at javax.media.j3d.NativePipeline.loadLibraries(Nativ ePipeline.java:157)
at javax.media.j3d.MasterControl.loadLibraries(Master Control.java:987)
at javax.media.j3d.VirtualUniverse.<clinit>(VirtualUn iverse.java:299)
at chess.Gui.<init>(Gui.java:31)
at chess.Gui.main(Gui.java:129)
... 5 more
will be fun^^
-
Re: using getResource
Glad you've got part of your problems solved. On to bigger and better!
- 02-25-2013, 10:54 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: using getResource
Eclipse compiles any java files in its list of source directories and plonks them in the bin directory.
It also takes any other non-Java files and transfers them as well.
So the usual way for handling extra resources is to have a directory to hold these images etc, and to make that directory a source directory (right-click the folder -> Build Path -> Use as Source Folder).Please do not ask for code as refusal often offends.
- 02-26-2013, 02:42 AM #11
Member
- Join Date
- Jun 2011
- Posts
- 11
- Rep Power
- 0
Re: using getResource
yea, got it thank you. I didn't know that before. The weird thing was when I chose to add a new source folder, it didn't appear in the directory, in my workspace. So I created it again just on the filesystem in the project folder (same location) but it didn't transfer the images to the jar. It wasn't until I actually dragged them in to my eclipse project that it actually recognized it. Maybe this is a bug, on the version of linux I'm using or something I don't know. Got it though. Thanks again all
- 02-26-2013, 09:43 AM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: using getResource
Eclipse does get out of synch with the workspace a lot, if stuff goes on outside of Eclipse.
To be honest, it just loses its mind sometimes.
We have several "magical" routines we use here in order to get it back (this is with it interacting with things like Maven)...I doubt any of them are actually fixing Eclipse, but they make us feel better.
:)Please do not ask for code as refusal often offends.
Similar Threads
-
Issue with getClass().getClassLoader().getResource(...) finding image file URL
By shall in forum NetBeansReplies: 3Last Post: 04-16-2012, 10:34 AM -
getClass().getResource()
By forwardbias in forum AWT / SwingReplies: 11Last Post: 03-29-2012, 03:10 AM -
How to change FileOutputStream to class.getResource()
By gomdohri in forum New To JavaReplies: 1Last Post: 10-27-2011, 10:13 AM -
ClassLoader getResource Problem
By sh4dyPT in forum New To JavaReplies: 5Last Post: 04-01-2010, 08:47 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks