More Java Woes

Discussion in 'General Off-Topic Chat' started by jonthedit, May 20, 2014.

  1. jonthedit
    OP

    jonthedit GBAtemp Advanced Maniac

    Member
    1,691
    438
    May 30, 2011
    Bangladesh
    Foxi4
    I've been working on my first ever game in Java and am happy to say it is nearly complete!
    I'm using Eclipse IDE and JRE 8 + JDK 8. Everything runs perfect in Eclipse, however my exported JAR does not run. Instead I get an "A Java Exception has occured." message popup.
    I did some research and found you can run JAR's in CMD to find out what is causing the exception. So, here is the problem:

    C:\Users\Marduk\Desktop>java -jar frog.jar
    Exception in thread "main" java.lang.ExceptionInInitializerError
    Caused by: java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(Unknown Source)
    at Menu.loadBufferedImage(Menu.java:334)
    at Menu.<init>(Menu.java:56)
    at RunGame.<clinit>(RunGame.java:3)


    Don't mind the RunGame part, it is not important it just calls everything (the main method)
    The REAL problem is line 334, my BufferImage method.

    So, here it is:
    Code:
    private BufferedImage loadBufferedImage(String string) {
        try {
            BufferedImage bi = ImageIO.read(this.getClass().getResource(string));
            return bi;
        } catch (IOException e) {
        }
        return null;
    }
    Again, this works perfectly in Eclipse. I even tried giving 'bi' a permanent one image, but no luck! It certainly messes up my images in my game while run the IDE though! (A sign that it is working)

    Code:
    private BufferedImage loadBufferedImage(String string) {
        try {
            BufferedImage bi = ImageIO.read(this.getClass().getResource("\\bggfx\\FrogGierStartScreengroundPanel_sd.png"));
            return bi;
        } catch (IOException e) {
        }
        return null;
    }
    For the sake of you understanding what is wrong with line 56-58, here is my code starting with Menu() in the class Menu.
    Code:
    public Menu() {
    int screenRes = Toolkit.getDefaultToolkit().getScreenResolution();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    ExitColor=Color.BLUE;
    InfoColor=Color.BLUE;
    MultiColor=Color.BLUE;
    StartColor=Color.BLUE;
    t = new Thread();
    t.start();
    //Main Menu Size and Set Visible'
    //Loop check getScreenSize() if 1600x900 then BgPic becomes BgResizedUser (controlled by loop)
    setFocusable(true);
    BgPanel = loadBufferedImage("\\bggfx\\FrogGierStartScreengroundPanel_sd.png");
    BgPic = loadBufferedImage("\\bggfx\\FrogGierStartScreenBackground.png");
    GameBG = loadBufferedImage("\\bggfx\\map.png");
    switch(screenRes)...
    
    FYI, I've also tried
    BgPanel = loadBufferedImage("/bggfx/FrogGierStartScreengroundPanel_sd.png");
    rather than \\, it produces the same effect.
    So what is wrong? It is not the images... They load perfectly in Eclipse! Maybe the code on lines 56-58 is wrong though? Looking for any suggestions!
    And yes, I'm exporting all my resources into the JAR when I "EXPORT" it in Eclipse.

    I can get it to run but only if I take away all images that use loadBufferImage...
    Very strange.
    Also audio does not play... Seems like it can't find the resources :|

    Most Likely this is the culprit:
    at javax.imageio.ImageIO.read(Unknown Source)
     
  2. Psionic Roshambo

    Psionic Roshambo GBAtemp Advanced Maniac

    Member
    1,821
    672
    Aug 12, 2011
    United States
    Not sure about this, since I am not a java programmer at all (My experience is with C++ so at least it looks semi close... lol)

    This page may help? (It's almost a stab in the dark from me, but I wan't to help lol)

    http://stackoverflow.com/questions/5713096/javax-imageio-iioexception-for-apparently-no-reason

    Down in the comments is where I suspect part of the problem is solved? Again I am outside my comfy zone on this one so if it's dumb just tell me to bugger off :)
     
  3. jonthedit
    OP

    jonthedit GBAtemp Advanced Maniac

    Member
    1,691
    438
    May 30, 2011
    Bangladesh

    I found that post on STOFLW earlier, sadly it did not apply to my issue or I misunderstood what his solution was.
    I just tried getting rid of my folders in my "res" source folder because they were not officially marked as "source folders".
    Nothing changed. :|
    Thanks for the reply though.

    OK! The problem is definitely with loading resources because my after removing all my image references the program loads but my audio does not play.
    It says "null" in cmd, which is the audio.
    Definitely a resource issue!
    Any ideas?
    I'm beginning to think JRE8/JDK8 is responsible!
     
    Psionic Roshambo likes this.
  4. Foxi4

    Foxi4 On the hunt...

    pip Reporter
    23,529
    21,457
    Sep 13, 2009
    Poland
    Gaming Grotto
    Try:
    Code:
    BufferedImage bi = ImageIO.read(getClass().getResource(string));
    return bi;
    ...and use single slashes - you're using double slashes in your strings, why?
    Code:
    BgPanel = loadBufferedImage("\\bggfx\\FrogGierStartScreengroundPanel_sd.png"); //Why?
    Mind you, you're asking the worst person in the universe - I temporarily gave up on Java and went back to C/C++. :P
     
    Psionic Roshambo likes this.
  5. jonthedit
    OP

    jonthedit GBAtemp Advanced Maniac

    Member
    1,691
    438
    May 30, 2011
    Bangladesh
    As stated in OP I tried / and \\ both produce the same effect.
    It does not seem to be just an image issue because even the audio won't play if I disable the images to allow the program to run.

    Audio becomes 'null' for whatever reason. This is really weird because my friend used the same buffered image code but his works... His program is running on JDK and JRE 7 though.

    The code is identical too... Hmm

    I'll give your idea a try, and if I fail I'm going to to try compiling with JDK 7 and running on JRE 7.


    No luck with the code you suggested.
    Also JRE 7/JDK 7 does not make a difference, same errors but only if the JAR file is exported and ran. (JRE 7 actually breaks a bit more code in the IDE)

    I'm really stumped :|

    Ok! So it is DEFINITELY this:
    private BufferedImage loadBufferedImage(String string) {
    try {
    BufferedImage bi = ImageIO.read(getClass().getResource(string));
    return bi;
    } catch (IOException e) {
    System.err.println(e.getMessage());
    }
    return null;

    }
    Because this:
    private BufferedImage loadBufferedImage(String string) {
    try {
    BufferedImage bi = ImageIO.read(getClass().getResource("/map.png"));
    return bi;
    } catch (IOException e) {
    System.err.println(e.getMessage());
    }
    return null;

    }

    Works, but is (obviously) only that image.
     
    Psionic Roshambo likes this.
  6. matt123337

    matt123337 Advanced Member

    Newcomer
    62
    16
    Mar 25, 2014
    Canada
    the bggfx folder is inside the jar correct? If not then that's your problem, as the getResource(String s) method is returning null (as in it can't lcoate the input)

    Also, I see that you tried both, but it's a lot easier to read if you just use a single slash.
     
  7. Linkiboy

    Linkiboy GBAtemp Testing Area

    Member
    4,888
    37
    May 14, 2006
    United States
    From your description it sounds like Eclipse is not properly including resources in the .jar file. A quick internet search came up with this: http://www.coderanch.com/t/526139/vc/Place-Resource-Files-Eclipse-Include


    Happy hacking.
     
  8. jonthedit
    OP

    jonthedit GBAtemp Advanced Maniac

    Member
    1,691
    438
    May 30, 2011
    Bangladesh
    I got rid of the bggfx folder so its now all the resources are in a folder "res" which is a "source folder".
    Yes it is inside the jar when it is compiled.


    Looks promising! I'll give it a try tomorrow!
    Thanks!
    No luck! :|
    All the resource files are in the bin folder.
     
  9. matt123337

    matt123337 Advanced Member

    Newcomer
    62
    16
    Mar 25, 2014
    Canada
    How odd, try it without a slash at the beginning.
     
  10. jonthedit
    OP

    jonthedit GBAtemp Advanced Maniac

    Member
    1,691
    438
    May 30, 2011
    Bangladesh
    Planning on giving this a try
     
  11. Ammako

    Ammako GBAtemp Guru

    Member
    6,404
    3,535
    Dec 22, 2009
    Canada
    What's the folder structure in your .jar?

    You could try using the absolute URL when calling loadBufferedImage for testing purposes (ex: "C:\Users\<user>\Desktop\game.jar\res\img.png"), I guess.

    I found this
    I'm not sure how correct this is (and not sure if it applies to Eclipse as well), but it could be worth investigating.

    This seemed interesting as well
     
  12. BORTZ

    BORTZ "Another stunning Van Gogh"

    Supervisor
    11,540
    14,059
    Dec 2, 2007
    United States
    Pittsburgh
    Please do not double post, it is against the rules. Thanks.
     
    EZ-Megaman likes this.
  13. Ammako

    Ammako GBAtemp Guru

    Member
    6,404
    3,535
    Dec 22, 2009
    Canada
    Okay, well I'm having the exact same problem and I can only load my file right if I give my program the absolute path to my file.

    Trying to load a midi to play sound, but I can't get it to work any other way than using absolute path.

    The annoying thing is that I got loading images to work fine in another program, but in your case (and in my case with my midi file) it won't work.

    Edit: Got my midi file loading working, but unfortunately I think my solution won't work with images. :/
     
  14. jonthedit
    OP

    jonthedit GBAtemp Advanced Maniac

    Member
    1,691
    438
    May 30, 2011
    Bangladesh
    Same here! How did you get the midi working? I will post a tutorial once I myself get this working.
    It's amazing that there is not a lot of information about this...
    Maybe it only applies to Eclipse SR2?
     
  15. Ammako

    Ammako GBAtemp Guru

    Member
    6,404
    3,535
    Dec 22, 2009
    Canada
    Code:
    final Sequencer sequencer = MidiSystem.getSequencer();
    sequencer.open();
    InputStream is = new BufferedInputStream(this.getClass().getResourceAsStream("birabuto.mid"));
    sequencer.addMetaEventListener(new MetaEventListener() {
    	@Override
    	public void meta(MetaMessage msg) {
    		if (msg.getType() == 0x2F) { // End of track
    			// Restart the song
    			sequencer.setTickPosition(0);
    			sequencer.start();
    		}
    	}
    });
    sequencer.setSequence(is);
    
    However, BufferedImage doesn't accept BufferedInputStream so that probably won't help.

    I am able to get images to load properly by simply doing Image image = new Image("image.png"); (with image.png in the <default package>), but I'm assuming that you need them to be BufferedImage for some purpose or another.
     
  16. jonthedit
    OP

    jonthedit GBAtemp Advanced Maniac

    Member
    1,691
    438
    May 30, 2011
    Bangladesh

    Yes. I need BufferedImage to resize, etc. It works perfectly (as shown here: ) when run via Eclipse.
    http://gbatemp.net/threads/froggier-two-player-battle-game.366365/

    OffTopic: Shouldn't it be birabuto.midi?
     
  17. Ammako

    Ammako GBAtemp Guru

    Member
    6,404
    3,535
    Dec 22, 2009
    Canada
    nah, midi file extension is .mid

    Could you try this?

    Code:
    BufferedImage bi = ImageIO.read(this.getClass().getResourceAsStream("image.png"));
    
    (getResourceAsStream() instead of getResource())
     
  18. jonthedit
    OP

    jonthedit GBAtemp Advanced Maniac

    Member
    1,691
    438
    May 30, 2011
    Bangladesh
    Makes no difference. -works in eclipse just fine though.

    Same error via CMD
    <dir>java -jar frog.jar
    Exception in thread "main" java.lang.ExceptionInInitializerError
    Caused by: java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(Unknown Source)
    at Menu.loadBufferedImage(Menu.java:342)
    at Menu.<init>(Menu.java:57)
    at RunGame.<clinit>(RunGame.java:3)
     
  19. Ammako

    Ammako GBAtemp Guru

    Member
    6,404
    3,535
    Dec 22, 2009
    Canada
    Well this particular line of code seems to work just fine on my end, using NetBeans.

    If I change "image.png" to something that doesn't exist, build fails as expected.
    When image.png exists (right now it's in the same package as the java file), it can find it fine and build the .jar normally. I can then run it directly from NetBeans or run it manually by double-clicking the .jar
     
  20. jonthedit
    OP

    jonthedit GBAtemp Advanced Maniac

    Member
    1,691
    438
    May 30, 2011
    Bangladesh
    Good news! (sort of)
    As a temporary solution I can now run images and audio as long as they are in the same folder as the .jar
    Still not sure why packed content refuses to work.