Results 1 to 12 of 12
  1. #1
    joet1984 is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default 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!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: using getResource

    Where are the class files located? Consider placing the resources folder in a directory off of these guys.

  3. #3
    joet1984 is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default 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 :[

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: using getResource

    For example, a minimal program could look like so:

    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();
          }
       }
    }
    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:

    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

  6. #6
    joet1984 is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default 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

  7. #7
    joet1984 is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default 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!

  8. #8
    joet1984 is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default 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^^

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  10. #10
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default 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.

    ** This space for rent **

  11. #11
    joet1984 is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default 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

  12. #12
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default 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.

    ** This space for rent **

Similar Threads

  1. Replies: 3
    Last Post: 04-16-2012, 10:34 AM
  2. getClass().getResource()
    By forwardbias in forum AWT / Swing
    Replies: 11
    Last Post: 03-29-2012, 03:10 AM
  3. Replies: 1
    Last Post: 10-27-2011, 10:13 AM
  4. ClassLoader getResource Problem
    By sh4dyPT in forum New To Java
    Replies: 5
    Last Post: 04-01-2010, 08:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •