Results 1 to 6 of 6
  1. #1
    afraidofdark is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default manifest file problem

    hello, I made a simple game. my problem is that, netbeans automatically generate a jar file and manifest file, it also points main class correctly but my game involves some jpg files in folder named sprite, I also load them in code like this img.load("sprites/abc.jpg") however when I run jar file, java pops up errors that says it fails loading those resources, to overcome the problem I simply add my sprite folder in to the jar but java doesn't find them, what should I do ?
    thank you for your help.

  2. #2
    afraidofdark is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default

    some details,
    I created a java application, and a main class file, I also used resources in a folder called sprites then I build project, netbeans generated a jar file however when I run the jar, java cannot locate resources, even though sprite folder is near the jar file. It is not working even if I add sprite folder as a packge to the project ( this adds sprites to jar file), so how can I make an executable program (a jar file) that can access files in other folders ?

    code is like this

    _img = ImageIO.read(new File(fileName));

    fileName = "sprites/abc.png"

    code works without any problem through ide, problem occures when I run jar file

  3. #3
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    It's reasonable your approach doesn't work: there is no File called "sprites/abc.png" on the file system, how should ImageIO find it?

    You have to work with resources... MyObject.class.getResource ("sprites/abc.png") kind of syntax, but then, you have to set up your directory structure just right... take a look here

  4. #4
    afraidofdark is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default

    thank you, I changed the code and make it something like this

    URL url = getClass().getResource(fileName);
    _img = new ImageIcon(Toolkit.getDefaultToolkit().getImage(url ));

    now program is working as jar package too, but I still wonder if there is a way to open files through I/O class when program is being ran as jar package

    ->Game
    ---->jarPackage
    ---->Sprite
    ------->asd.jpg

    _img = ImageIO.read(new File("Sprite/asd.jpg"));

    or do I have to enter an absolute path ? like c/:...Game/Spirte/asd.jpg
    Last edited by afraidofdark; 03-26-2010 at 11:39 PM.

  5. #5
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    The cleanest way to do this is to include all resources in the jar file, rather than trying to mix and match between the jar and contents elsewhere on the file system.

    If there is some valid reason that you don't want to put al your resources in the jar file, then you need to come up with some other way to find external resources. The two common ways of doing that are to have an installer define them in preferences, or to discover the location of the jar file and use a relative path.

    The second approach get's complicated if the jar is not an actual file, but a URL -...which is why you should include all resources in the jar file unless there is a compelling reason not to.

  6. #6
    afraidofdark is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

Similar Threads

  1. Jar File Problem
    By Moncleared in forum New To Java
    Replies: 10
    Last Post: 10-18-2009, 07:43 AM
  2. Jar File Manifest
    By hitmen in forum New To Java
    Replies: 6
    Last Post: 03-11-2009, 04:08 AM
  3. Manifest file in executable jars
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-15-2007, 08:16 PM
  4. problem with jar file pls help
    By jinu5 in forum New To Java
    Replies: 0
    Last Post: 08-15-2007, 10:41 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
  •